Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement automatic timestamps for record changes (SQL Server)

I am used to using "changed_at" fields in my MySQL database that Ruby on Rails automatically updates when changing a record.

Now I am using ASP.NET MVC with SQL Server 2008 and I am wondering, how would I proceed to get the same functionality. Does SQL Server have an option for auto updating a field when updating?

like image 706
Peter Avatar asked May 22 '09 18:05

Peter


3 Answers

You could use a trigger or just update it in an sp that does the update.

like image 32
CSharpAtl Avatar answered Nov 23 '22 08:11

CSharpAtl


do this...

UPDATE YourTable
    SET Column1=@...
        ,@Column2=@....
        ,@Column3=@...
        ,changed_at=GETDATE()
    WHERE ...
like image 39
KM. Avatar answered Nov 23 '22 09:11

KM.


The "timestamp" data type gives you a binary value that automatically gets updated every time your field changes, but it won't give you a nice date/time value.

like image 191
David Avatar answered Nov 23 '22 10:11

David