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 > Need help with trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-10, 02:32
malle86 malle86 is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
Need help with trigger

I am a newbie with db2 and would appreciate some help to get my trigger to work.

Code written as follows:
Code:
CREATE TRIGGER update_sal
	BEFORE INSERT ON Work
	FOR EACH ROW
	BEGIN
	UPDATE Staff SET salary = salary * 1.02 WHERE Work.sno = Staff.sno;
END;
Level DB2 v9.7.200.358

Thank you in advance
Reply With Quote
  #2 (permalink)  
Old 12-10-10, 02:49
wilsonfv wilsonfv is offline
Registered User
 
Join Date: Apr 2009
Posts: 42
check db2 manual, you miss a "referencing" option in you command.
Reply With Quote
  #3 (permalink)  
Old 12-10-10, 04:45
malle86 malle86 is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
Quote:
Originally Posted by wilsonfv View Post
check db2 manual, you miss a "referencing" option in you command.
Thank you. So I added the referencing but still can't seem to get it to work.

Right now my code looks as following:

Code:
CREATE TRIGGER update_sal
	BEFORE INSERT ON Work
	REFERENCING NEW ROW AS new OLD ROW AS old
	FOR EACH ROW
	BEGIN
	UPDATE Staff SET new.salary = old.salary * 1.02 WHERE new.Work.sno = old.Staff.sno;
	END;
Reply With Quote
  #4 (permalink)  
Old 12-10-10, 05:27
wilsonfv wilsonfv is offline
Registered User
 
Join Date: Apr 2009
Posts: 42
what do you mean by it doesnt work. any error occurred?
besides, I believe your sql wont even execute successfully. cause you are building a trigger on the table "Work", but in the trigger you update another table!
Reply With Quote
  #5 (permalink)  
Old 12-10-10, 05:46
malle86 malle86 is offline
Registered User
 
Join Date: Dec 2010
Posts: 3
Yes, the error occurs at the end, unexpected end-of-statement.

This is a school assignment which I can't seem to solve, I am supposed to update the other table.

Any suggestions on how to get around this problem? I have to solve it by trigger.

Edit: sno is staff number, when a staff member gets another "work" (project), his salary will be increased by 2%. Salary is saved in Staff table.

Last edited by malle86; 12-10-10 at 05:50.
Reply With Quote
  #6 (permalink)  
Old 12-10-10, 09:07
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Make it an AFTER trigger, not before. And get rid of the BEGIN and END lines.

Andy
Reply With Quote
  #7 (permalink)  
Old 12-10-10, 10:19
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
also, you need to change delimeter to something else for the END

Quote:
UPDATE Staff SET new.salary = old.salary * 1.02 WHERE new.Work.sno = old.Staff.sno;
END~
Dave
Reply With Quote
  #8 (permalink)  
Old 12-10-10, 10:23
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by dav1mo View Post
also, you need to change delimeter to something else for the END


Dave
If you remove the BEGIN and END lines you do not have to do this. There is only one statement in the BEGIN-END so it is unnecessary.

Andy
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