How to: Create trigger for auto update modified date with SQL Server 2008 How to: Create trigger for auto update modified date with SQL Server 2008 sql-server sql-server

How to: Create trigger for auto update modified date with SQL Server 2008


My approach:

  • define a default constraint on the ModDate column with a value of GETDATE() - this handles the INSERT case

  • have a AFTER UPDATE trigger to update the ModDate column

Something like:

CREATE TRIGGER trg_UpdateTimeEntryON dbo.TimeEntryAFTER UPDATEAS    UPDATE dbo.TimeEntry    SET ModDate = GETDATE()    WHERE ID IN (SELECT DISTINCT ID FROM Inserted)