Anyone have a good example on how to use a trigger?
All I want is to update a field in a table (table a) depending on what value has been inserted in another a particular field in another table (table b).
It should just be a case of defining a trigger on table b for the INSERT action which performs the necessary update. For DB2 you'd be looking at something like...
Code:
create trigger trigger_name
after insert on table_b
referencing new as newrow
for each row mode db2sql
update table_a set column_name = newrow.column_name2
I think there is a temporary table called inserted and another called deleted available inside the trigger, deleted would hold the original value but be careful if it's not a per record trigger.
Cheers, the book I've got here uses a "REFERENCES", but this doesn't seem to work under SQL2005, likewise SQL2000 has a BEFORE or AFTER clause, but 2005 assumes you're choosing a BEFORE unless you specifically say "AFTER".
OK well if anyone's interested, SQL2005 has a FANTASTIC feature called "INSTEAD OF" where you enter this after the creation of the trigger and put your code. Your code is then run INSTEAD OF what SQL server would normally do!
Brilliant!
Stupid example is:
Create Trigger NastyPasty ON Order.Items
INSTEAD OF
FOR INSERT
AS
BEGIN
DELETE * FROM Order.Items
END
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.