If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Create Trigger on Update of Tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-14-08, 18:00
db2user db2user is offline
Registered User
 
Join Date: Dec 2002
Posts: 123
Create Trigger on Update of Tables

Hi,

I have three tables :

StatusTable --> has columns empid, haschanged

EmpTable --> has columns empid, empname, descr

SalaryTable --> has columns empid, salary

I want to create a trigger that updates the column 'haschanged' in the Status table only if the values of 'empname', 'descr' or 'salary' change after an update.

what's the best way to do this? thanks!
Reply With Quote
  #2 (permalink)  
Old 10-14-08, 18:39
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Well, just create an AFTER UPDATE trigger. What exactly is the problem there?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 10-14-08, 18:53
db2user db2user is offline
Registered User
 
Join Date: Dec 2002
Posts: 123
So far I have this and it works -->

CREATE TRIGGER EmpUpdate_tgr
AFTER UPDATE OF empname, descr ON EmpTable
REFERENCING NEW AS N OLD AS O
FOR EACH ROW
WHEN( (N.empname != O.empname) OR (N.descr != O.descr) )
UPDATE StatusTable SET haschanged = 't' WHERE empid = N.empid;

But how can I include the Salary table as a part of this trigger? Is it possible to do that?
Reply With Quote
  #4 (permalink)  
Old 10-15-08, 02:15
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by db2user
how can I include the Salary table as a part of this trigger? Is it possible to do that?
No, you'll need two triggers, one on each table.
Updating the "haschanged" flag twice shouldn't be problematic, I suppose.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #5 (permalink)  
Old 10-15-08, 12:52
db2user db2user is offline
Registered User
 
Join Date: Dec 2002
Posts: 123
No, changing the 'haschanged' flag twice isn't a problem at all... how about nested triggers? What are they used for...could they be used in this case? Thanks!
Reply With Quote
  #6 (permalink)  
Old 10-15-08, 18:38
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by db2user
How about nested triggers? What are they used for...could they be used in this case?
In this case you want to trigger two different actions, viz. updates in EmpTable and updates in SalaryTable. For that purpose you'll need at least two triggers.
Since two fairly simple triggers will do the job, I don't see any need for more complex triggers.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #7 (permalink)  
Old 10-15-08, 18:46
db2user db2user is offline
Registered User
 
Join Date: Dec 2002
Posts: 123
thank you.. i created two triggers and it works just fine..
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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