Brian Webster Brian Webster - 11 months ago 41
SQL Question

sql - Update Trigger to populate a ModifyDate field

I'm looking for an example of a good update trigger to update the ModifyDate field of a table.

It would be nice to handle the case where an update command updated more than one record in the table.

Is there a good template or tutorial for this?


Here's a cut and paste (and rename, to protect the innocent) of one I wrote quite some time ago (aka it works):

CREATE TRIGGER dbo.TR_iu_MyTable__LastUpdated
 on dbo.MyTable
 after insert, update



    UPDATE dbo.MyTable
     set LastUpdated = getdate()
     where MyTableId in (select MyTableId from inserted)