更新时间:2023-09-23 11:13:58
要知道最后更新哪一行,您需要创建一个新列,是否会再次触发该触发器?键入 DATETIME
/ DATETIME2
并使用触发器进行更新。没有数据类型会在每次更新行时自动使用日期/时间信息自动更新。
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.
为避免递归,可以使用 UPDATE ()
子句,例如
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