测试一个动作是否是触发器中的更新

时间:2013-03-06 09:41:22

标签: sql-server sql-server-2008 triggers

我在删除/更新/插入操作时为表创建了一个触发器。

测试它是否是删除我使用此语句:

if exists(select * from deleted)

如果是插入内容:

if exists(select * from inserted)

但是如何测试它是否是更新

1 个答案:

答案 0 :(得分:1)

 SET @action = 'I'; -- Set Action to Insert by default.
    IF EXISTS(SELECT * FROM DELETED)
    BEGIN
        SET @action = 
            CASE
                WHEN EXISTS(SELECT * FROM INSERTED) THEN 'U' -- Set Action to Updated.
                ELSE 'D' -- Set Action to Deleted.       
            END
    END
    ELSE 
    BEGIN
        IF NOT EXISTS(SELECT * FROM INSERTED) -- New record inserted.
             SET @action = 'I';
    END