2011-10-18 35 views
0

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

我有点失去了如何使用该查询存在。

回答

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 
+0

我喜欢这个查询,但我可以使用If Exist,它可以有点更优雅。谢谢你的答案,但我会用这个查询。 –

+0

我不确定你的意思是“更优雅”,但我认为在这种情况下JOIN操作会更好。 –

相关问题