Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 更新时自动停用旧记录的触发器_Sql Server 2005_Triggers_Exists - Fatal编程技术网

Sql server 2005 更新时自动停用旧记录的触发器

Sql server 2005 更新时自动停用旧记录的触发器,sql-server-2005,triggers,exists,Sql Server 2005,Triggers,Exists,我正在更新记录。我需要创建一个触发器,它将自动停用旧记录。记录被更新到一个不同的新表中。新插入的记录和原始记录之间的值由两列匹配,比如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

我正在更新记录。我需要创建一个触发器,它将自动停用旧记录。记录被更新到一个不同的新表中。新插入的记录和原始记录之间的值由两列匹配,比如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。

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

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

我喜欢这个查询,但是我可以使用If-Exist,它可以更优雅一些。谢谢你的回答,我将使用这个查询。我不确定你所说的“更优雅”是什么意思,但我认为在这种情况下连接操作会更好。