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

后端 未结 1 1649
-上瘾入骨i
-上瘾入骨i 2020-11-29 02:37

It would be nice to know how to create a trigger that auto-updates the modifiedDate column in my SQL Server table:

Table TimeEntry

相关标签:
1条回答
  • 2020-11-29 03:10

    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_UpdateTimeEntry
    ON dbo.TimeEntry
    AFTER UPDATE
    AS
        UPDATE dbo.TimeEntry
        SET ModDate = GETDATE()
        WHERE ID IN (SELECT DISTINCT ID FROM Inserted)
    
    0 讨论(0)
提交回复
热议问题