我有一個觸發器應該在更新Employees
表時將新記錄插入審計歷史記錄表(EmployeeSalaryHistory
),觸發器設置爲開啓。觸發表更新觸發超過更新的行數
如果我在Employees
上執行UPDATE
,表中的所有行都被更新,則觸發器被調用的次數會多於正在更新的行數。
例如如果Employees
表中有三行,則INSERT
發生9次。
/*This UPDATE will cause the trigger to fire more than the number of rows in the Employees table.*/
UPDATE Employees SET Salary = Salary * 2
/* supposed to be fired whenever the salary of an employee is updated */
CREATE TRIGGER [dbo].[EmployeesUpdateSalary] ON [dbo].[Employees]
AFTER UPDATE
NOT FOR REPLICATION
AS
BEGIN
INSERT INTO EmployeeSalaryHistory(EmployeeID, NewSalary, OldSalary)
SELECT I.EmployeeID, I.Salary, D.Salary
From inserted I, deleted D
WHERE I.Salary <> D.Salary
END
也許發佈您的解決方案......作爲*附加編輯*到您的原始文章.......所以其他人可以看到你最終做了什麼。保持您的原始文本「完整」,以便清楚地看到原始問題。 – granadaCoder