It would be nice to know how to create a trigger that auto-updates the modifiedDate column in my SQL Server table:
Table TimeEntry
Id (PK)
UserId (FK)
Description
Time
GenDate
M开发者_如何学GoodDate
Trigger code:
+ TR_TimeEntry_UpdateModDate()
+ TR_TimeEntry_InsertGenDate()
An example for update ModDate would be nice.
My approach:
define a default constraint on the
ModDatecolumn with a value ofGETDATE()- this handles theINSERTcasehave a
AFTER UPDATEtrigger to update theModDatecolumn
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)
加载中,请稍侯......
精彩评论