EmployeeSalaryHistory
我有一个触发器,它应该在更新Employees
触发器设置为 ON 的表时将新记录插入审计历史表 ( )。
如果我UPDATE
对Employees
表中所有行的更新位置进行操作,则触发器被调用的次数多于正在更新的行数。
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