SQL Triggers - General help

~J~

~J~

Soldato
Joined
20 Oct 2003
Posts
7,558
Location
London
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).

Any pointers?
 
Post revival here!!

Managed to sort out my insert triggers no problems, but my update ones are proving more difficult.

Is there any way I can get the current value BEFORE the update so that I can use it to perform calculations before the new value is commited?
 
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".

Weird.
 
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

:D
 
Back
Top Bottom