Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a "last updated" column in a SQL Server 2008 R2 table?

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?

like image 885
Avrohom Yisroel Avatar asked Sep 08 '11 14:09

Avrohom Yisroel


People also ask

How do you add a new column to an existing column in SQL?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.

How do I add a new record to an existing table?

To insert records into a table, enter the key words insert into followed by the table name, followed by an open parenthesis, followed by a list of column names separated by commas, followed by a closing parenthesis, followed by the keyword values, followed by the list of values enclosed in parenthesis.

How do I get the latest updated record in SQL Server?

To get the last updated record in SQL Server: We can write trigger (which automatically fires) i.e. whenever there is a change (update) that occurs on a row, the “lastupdatedby” column value should get updated by the current timestamp.


2 Answers

To know which row was last updated, you need to create a new column of type DATETIME/DATETIME2 and 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.

ALTER TRIGGER dbo.SetLastUpdatedBusiness  ON dbo.Businesses  AFTER UPDATE -- not insert! AS BEGIN     IF NOT UPDATE(LastUpdated)     BEGIN         UPDATE t             SET t.LastUpdated = CURRENT_TIMESTAMP -- not dbo.LastUpdated!             FROM dbo.Businesses AS t -- not b!             INNER JOIN inserted AS i              ON t.ID = i.ID;     END END GO 

In modern versions you can trick SQL Server into doing this using temporal tables:

  • Maintaining LastModified Without Triggers

But this is full of caveats and limitations and was really only making light of multiple other similar posts:

like image 96
Aaron Bertrand Avatar answered Sep 19 '22 23:09

Aaron Bertrand


It's not that easy, unfortunately.

You can add a new DATETIME (or DATETIME2) field to your table, and you can give it a default constraint of GETDATE() - that will set the value when a new row is inserted.

Unfortunately, other than creating an AFTER UPDATE trigger, there is no "out of the box" way to keep it updated all the time. The trigger per se isn't hard to write, but you'll have to write it for each and every single table that should have that feature.....

like image 44
marc_s Avatar answered Sep 21 '22 23:09

marc_s