Home All Groups Group Topic Archive Search About

Update Trigger - how to cacptures only changes

Author
19 Aug 2005 4:35 AM
Ben
Hi,

I've created a trigger to capture 2 fields (f1 and f2).
I did and check by using If update(f1) or update(f2) then
only firing the trigger to create old and new image.
But, this trigger will be fired eventhounght the f1 and f2 value did not
change.
How do I check the changes before fire the update trigger.

Please advise.

Ben

Author
19 Aug 2005 6:20 AM
David Portas
Join the INSERTED and DELETED virtual tables in your trigger code and
compare the column values. Remember that a trigger fires once per update not
once per row - there may be more than one row in those tables.

--
David Portas
SQL Server MVP
--
Author
19 Aug 2005 2:23 PM
Ben
Hi David,

Where can I find a documentation or sample code on this ?

Thank you in advance.

Benny

Show quote
"David Portas" wrote:

> Join the INSERTED and DELETED virtual tables in your trigger code and
> compare the column values. Remember that a trigger fires once per update not
> once per row - there may be more than one row in those tables.
>
> --
> David Portas
> SQL Server MVP
> --
>
>
>
Author
19 Aug 2005 3:46 PM
ML
Read introduction here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_08_0lo3.asp

A few samples are available under "CREATE TRIGGER".


ML
Author
19 Aug 2005 10:40 PM
David Portas
Unfortunately the example code in the CREATE TRIGGER topic isn't very good.
The join will look something like this

....
FROM Inserted AS I
JOIN Deleted AS D
  ON I.key_col  = D.key_col
WHERE I.col1 <> D.col1
     OR I.col2 <> D.col2
   ...

assuming your columns are not nullable.

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button