473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trigger design issues

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,

Jul 14 '06 #1
3 1350
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)
@
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 14 '06 #2
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

Jul 14 '06 #3
Thanks guys.

I will test all solutions.

Jul 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Galina | last post by:
Hello I work with Oracle 9 database. I want to create a trigger using 2 tables: KEY_SKILLS_STUDENT and KEY_SKILLS. There are fields in KEY_SKILLS_STUDENT: KEY_SKILLS_ID, PORTFOLIO_RESULT and...
9
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
2
by: Net Virtual Mailing Lists | last post by:
Hello, If I have a rule like this: CREATE OR REPLACE RULE sometable_update AS ON UPDATE TO table2 DO UPDATE cache SET updated_dt=NULL WHERE tablename='sometable'; CREATE OR REPLACE RULE...
6
by: Jchick | last post by:
Im a newbie with a sql table in a MSDE database that contains fields of CustName, BrokerName, Type, Status. I am trying to write a trigger that does this: 1. When a new record in a table is...
2
by: tolcis | last post by:
I have a trigger that should be execute on each row insert and only if appcode = 'I' and datasent = 0. It should execute a DTS package. The DTS package by itself runs about 6 seconds. Trigger...
2
by: dean.cochrane | last post by:
I have inherited a large application. I have a table which contains a hierarchy, like this CREATE TABLE sample_table( sample_id int NOT NULL parent_sample_id int NOT NULL ....lots of other...
3
by: Yas | last post by:
Hi everyone I am trying to create a DELETE Trigger. I have 2 tables. Table1 and Table2. Table 2 has all the same fields and records as Table1 + 1 extra column "date_removed" I would like that...
4
by: Peter | last post by:
ASP.NET 3.5 I have a web page and this web page has a DataList, when user hovers the mouse over a picture in the datalist a popup appers, this popup is a User Control and it has a cancel button....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.