Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / July 2006

Tip: Looking for answers? Try searching our database.

Trigger design issues

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michel Esber - 14 Jul 2006 15:59 GMT
Hello,

LUW V8 FP 12.

My application issues INSERT statements into a table:

INSERT INTO TABLE_A values (ID, TIMESTAMP, VALUE1), (ID, TIMESTAMP,
VALUE2), (ID, TIMESTAMP, VALUE3) ...

There is an AFTER INSERT trigger on TABLE_A that inserts all rows from
the last inserted (ID, TIMESTAMP) into another table.

Here is the code:

CREATE TRIGGER TRG.INS_IOSTAT
 AFTER INSERT ON TABLE_A
 REFERENCING
   NEW AS n
 FOR EACH ROW
 MODE DB2SQL

 begin atomic
 delete from TABLE_B where ID=n.ID and TIMESTAMP!=n.timestamp;
 insert into TABLE_B values (n.Id, n.TIMESTAMP, n.VALUE);
 end@

TABLE_A keeps all inserted rows, and TABLE_B is meant to keep only the
last TIMESTAMP inserted in TABLE_A.

I know this solution is fairly poor. FOR EACH ROW, a delete statement
is used to remove old data from TABLE_B. I eventually see lock-waits on
this table.

I´ve read the DOCs and I understand that a better solution is to use
FOR EACH STATEMENT triggers referencing all rows with NEW_TABLE.
But there are no samples on how to use NEW_TABLE inside the trigger
code. Do I need to open a cursor to fetch its rows?

Is there a simple way to change this "for each row" trigger to use a
"for each statement" ? Any other ideas?

Thanks in advance,
Serge Rielau - 14 Jul 2006 16:43 GMT
CREATE TRIGGER TRG.INS_IOSTAT
  AFTER INSERT ON TABLE_A
  REFERENCING
    NEW TABLE AS n
  FOR EACH STATEMENT
  MODE DB2SQL

  begin atomic
  delete from TABLE_B where (ID, TIMESTAMP) IN (SELECT ID, timestamp
FROM n);
  insert into TABLE_B SELECT Id, TIMESTAMP, VALUE FROM n;
  end@

Here is what may be a faster solution:
CREATE TRIGGER TRG.INS_IOSTAT
  AFTER INSERT ON TABLE_A
  REFERENCING
    NEW TABLE AS n
  FOR EACH STATEMENT
  MODE DB2SQL
  MERGE INTO TABLE_B AS T USING N ON N.ID = T.ID
  WHEN MATCHED THEN UPDATE SET (TIMESTAMP, VALUE)
                               = (N.TIMESTAMP, N.VALUE)
  WHEN NOT MATCHED THEN INSERT VALUES(ID, TIMESTAMP, VALUE)
@
Signature

Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ondemandbusiness/conf2006/

mirof007 - 14 Jul 2006 16:57 GMT
Have you looked a the samples in $DB2INSTHOME/sqllib/samples? In any
case, I think the following should work:

CREATE TRIGGER TRG.INS_IOSTAT
 AFTER INSERT ON TABLE_A
 REFERENCING NEW_TABLE AS N
 FOR EACH STATEMENT
 MODE DB2SQL

 BEGIN ATOMIC
   DELETE FROM TABLE_B
     WHERE TABLE_B.ID IN (SELECT ID FROM N);
   INSERT INTO TABLE_B SELECT ID, TIMESTAMP, VALUE FROM N;
 END@

Regards,
Miro
Michel Esber - 14 Jul 2006 21:23 GMT
Thanks guys.

I will test all solutions.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.