Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 12th, 2005, 11:38 AM
tomtailor@freesurf.fr
Guest
 
Posts: n/a
Default Before insert Trigger

Hello!

I have a before insert Trigger and I want to catch if there is a
duplicate Key Error. If the Key already exists I'd like to update else
insert the row.

OK I am at the point I did the updates but if I raise an Error the
update get rolled back.

What do I have to define in the Trigger after the updates?

THX!
Christian Meier

  #2  
Old November 12th, 2005, 11:38 AM
tomtailor@freesurf.fr
Guest
 
Posts: n/a
Default Re: Before insert Trigger

OK I saw its not possible to update in a before trigger so I have to
find another workaround ...

  #3  
Old November 12th, 2005, 11:38 AM
Serge Rielau
Guest
 
Posts: n/a
Default Re: Before insert Trigger

tomtailor@freesurf.fr wrote:[color=blue]
> Hello!
>
> I have a before insert Trigger and I want to catch if there is a
> duplicate Key Error. If the Key already exists I'd like to update else
> insert the row.
>
> OK I am at the point I did the updates but if I raise an Error the
> update get rolled back.
>
> What do I have to define in the Trigger after the updates?[/color]
You won't be able to bend an INSERT to a MERGE using a BEFORE trigger.
The only way I can think up to achieve what you want is to use an
INSTEAD OF trigger. Today these triggers are defined on VIEWs only.

So: CREATE VIEW T AS SELECT * FROM TBASE;
CREATE TRIGGER trg1 INSTEAD OF INSERT ON T
REFERENCING NEW AS n FOR EACH ROW MODE DB2SQL
MERGE INTO TBASE
USING VALUES(N.PK, N.c1, N.c2) AS S(PK, C1, C2)
ON TBASE.PK = S.PK
WHEN NOT MATCHED THEN INSERT VALUES(S.PK, S.C1, S.C2)
WHEN MATCHED THEN UPDATE
SET (C1, C2) = (S.C1 + TBASE.C1, S.C2. + TBASE.C2)

That ought to do....
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
  #4  
Old November 12th, 2005, 11:40 AM
tomtailor@freesurf.fr
Guest
 
Posts: n/a
Default Re: Before insert Trigger

ok, took some time but now it works ...

thx!

Christian

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,174 network members.