473,503 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trigger logic

I have been reading up on triggers and I am still unclear as to the
best way to accomplish the following:

Each row in table A has an account number field. After insert of record
1 into table A, if the value of the acccount number exists in table B,
I need to update that corresponding record in table B, otherwise I need
to insert the record into table B. I know that I need an insert
trigger, so that after each insert into table A, an event is triggered.
What I am unsure about is what type of logic is actually allowed in a
trigger. How would I accomplish the checking to see if the record
exists in table B, if so do an update, otherwise do an insert? What is
tbe best way, in a trigger, to tell if a record with this account
number exists in table B. Thanks.

Nov 12 '05 #1
4 3857
mi***********@gmail.com wrote:
I have been reading up on triggers and I am still unclear as to the
best way to accomplish the following:

Each row in table A has an account number field. After insert of record
1 into table A, if the value of the acccount number exists in table B,
I need to update that corresponding record in table B, otherwise I need
to insert the record into table B. I know that I need an insert
trigger, so that after each insert into table A, an event is triggered.
What I am unsure about is what type of logic is actually allowed in a
trigger. How would I accomplish the checking to see if the record
exists in table B, if so do an update, otherwise do an insert? What is
tbe best way, in a trigger, to tell if a record with this account
number exists in table B. Thanks.

Which version of DB2 are you on?
Her eis one way of doing it:
CREATE TRIGGER trg1 AFTER INSERT ON B
REFERENCING NEW_TABLE AS NTAB FOR EACH STATEMENT MODE DB2SQL
MERGE INTO A USING NTAB
ON A.pk = NTAB.pk
WHEN NOT MATCHED THEN INSERT (NTAB.pk, .....)
WHEN MATCHED THEN UPDATE SET A.c1 = NTAB.c1, ....;

In older versions of DB2 (e.g. pre V8.1.2)

CREATE TRIGGER ....
REFERENCING NEW AS n FOR EACH ROW
BEGIN ATOMIC
DECLARE cnt BIGINT;
UPDATE A SET A.c1 = N.c1, ... WHERE A.pk = N.pk;
GET DIANOSTICS cnt = ROW_COUNT;
INSERT INTO A SELECT * FROM TABLE(VALUES (n.pk, n.c1, ..) AS X
WHERE cnt = 0;
END
%
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Thats great. Thanks a lot. Looks like what I need.

Nov 12 '05 #3
I think the answer is no, but would it change anything, if it were
possible for a row to exist multiple times with the same account number
in the table that is being updated? Does the "ON" portion of the MERGE
statement need to be on primary key/? Thanks

Nov 12 '05 #4
mi***********@gmail.com wrote:
I think the answer is no, but would it change anything, if it were
possible for a row to exist multiple times with the same account number
in the table that is being updated? Does the "ON" portion of the MERGE
statement need to be on primary key/? Thanks

MERG Ecan update multiple traget rows from one source.
But each individual row in the target must be updated/inserted at most once.
That is if you could have dups in your INSERT statement, e.g.:
INSERT INTO A(pk, c1) VALUES (1, 1), (1, 2), (2, 3);
Then you need to aggregate NEWTAB in the USING clause. E.g. by doing a
SUM(c1) GROUP BY pk.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5

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

Similar topics

8
8583
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
2
7157
by: Rigs | last post by:
Hi, I'm a SQL Server newbie, so I'd appreciate if someone would tell me if this is possible. I'm running SQL Server 2000 on Win2k Server I have one table with a large number of columns. I...
6
6531
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...
4
4159
by: Alexander Pope | last post by:
I am using db2 udb v8.2 AIX I have created trigger, however I am not confident it meets industry standards. If I make it fail, I cant tell from the message where it is failing. what can I add to...
7
3354
by: yoyo | last post by:
Is there anyway to delay a trigger from firing for a few seconds? I have an after insert trigger on table1, which selects information from table2 and table3. Obviously it cannot fire unless the...
12
4737
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
5
5331
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
2
2375
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...
7
3273
by: Shane | last post by:
I have been instructed to write a trigger that effectively acts as a foreign key. The point (I think) is to get me used to writing triggers that dont use the primary key(s) I have created the...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
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
7328
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...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.