I have a table in my SQL Server 2008 R2 database, and would like to add a column called LastUpdated, that will automatically be changed every time the row is updated. That way, I can see when each individual row was last updated.
It seems that SQL Server 2008 R2 doesn’t have a data type to handle this like earlier versions did, so I’m not sure of the best way to do it. I wondered about using a trigger, but what would happen when the trigger updated the row? Will that fire the trigger again, etc?
To know which row was last updated, you need to create a new column of type
DATETIME/DATETIME2and update it with a trigger. There is no data type that automatically updates itself with date/time information every time the row is updated.To avoid recursion you can use the
UPDATE()clause inside the trigger, e.g.In modern versions you can trick SQL Server into doing this using temporal tables:
But this is full of caveats and limitations and was really only making light of multiple other similar posts:
Columns