In your trigger, you have two pseudo-tables available, Inserted
and Deleted
, which contain those values.
In the case of an UPDATE, the Deleted
table will contain the old values, while the Inserted
table contains the new values.
So if you want to log the ID, OldValue, NewValue
in your trigger, you'd need to write something like:
CREATE TRIGGER trgEmployeeUpdate
ON dbo.Employees AFTER UPDATE
AS
INSERT INTO dbo.LogTable(ID, OldValue, NewValue)
SELECT i.ID, d.Name, i.Name
FROM Inserted i
INNER JOIN Deleted d ON i.ID = d.ID
Basically, you join the Inserted
and Deleted
pseudo-tables, grab the ID (which is the same, I presume, in both cases), the old value from the Deleted
table, the new value from the Inserted
table, and you store everything in the LogTable
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…