I have a trigger that is supposed to insert a new record into a audit history table (EmployeeSalaryHistory
) upon the updating of the Employees
table that the trigger is set to be on ON.
If I do an UPDATE
on Employees
where all the rows in the table are updated the trigger is called more times than the number of rows that are being updated.
e.g. if there are three rows in the Employees
table the INSERT
happens 9 times.
/*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