This will no doubt be old hat for anyone who’s worked with SQL, but the other day I was going through some code snippets and came across this little gem.
I know I got it off a blog post somewhere, but never recorded where, so to anyone who’s ever posted this…Thanks!
This is pretty generic, if you only want to update the date when a certain field changes, you’ll have to modify a bit.
But if all you want is a field that updates every time a row updates, then this is your trigger.
SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON GO CREATE TRIGGER [dbo].[NameOfTable] ON [dbo].[NameOfTable] FOR UPDATE AS BEGIN IF NOT UPDATE(datefieldname) UPDATE dbo.NameOfTable SET datefieldname=GETDATE() WHERE id IN (SELECT id FROM inserted) END