2011-10-18 37 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操作會更好。 –

相關問題