473,468 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Create 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 3854
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
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
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
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
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
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
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
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
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
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.