1

date_modified我正在一个应该更新列的表上创建一个触发器。下面的代码是最好的方法吗?我这样做对吗?

IF EXISTS(SELECT * FROM DELETED) --checking if this is an update, not insert
BEGIN
IF NOT(UPDATE(date_modified)) -- checking if desired column was not updated
BEGIN
    DECLARE @updatedID int
    SELECT @updatedID = ID FROM deleted -- fetching updated record ID
    UPDATE table SET date_modified=GETDATE() WHERE ID=@updatedID -- updating desired column
END
4

1 回答 1

0

您可以简单地使用它而不是声明变量

UPDATE table SET date_modified=GETDATE() WHERE ID in 
(SELECT ID FROM deleted)
于 2012-07-19T11:15:25.413 回答