在续订时自动停用旧记录的触发器

时间:2011-10-18 19:48:26

标签: sql-server-2005 triggers exists

我正在更新记录。我需要创建一个自动停用旧记录的触发器。记录更新为新表不一样。新插入记录和原始记录之间的值匹配2列,比如说col1和col2

ALTER TRIGGER TR_On_Renewed_Customer
ON CustomerTable2
FOR INSERT
AS
// psudeo code
// Deactive the old record in customertable1 
// if match is found between CustomerTable2 and 
// CustomerTable1 based on col1 and col2, then update Active ='No'

我有点迷失了如何在此查询中使用EXIST。

1 个答案:

答案 0 :(得分:0)

关键是使用特殊的Inserted表,其中包含仅受INSERT操作影响的行,这些行导致触发器触发。

UPDATE c1
    SET Active = 'No'
    FROM Inserted i
        INNER JOIN CustomerTable1 c1
            ON i.col1 = c1.col1
                AND i.col2 = c1.col2