shiggyshag
Programmer
Hi
I have a table where all the columns are updated even if there is no change to some of the columns from an SP
Now I have the following trigger but because I update all the columns it runs can I check the values are different then run it if they are?
ALTER TRIGGER TriggerContacts
ON dbo.tContacts
FOR update AS
IF Update(Forename)
BEGIN
INSERT INTO tContactHistory (ContactID, UpdateDate, ColumnName, oldvalue, newvalue, UserID)
SELECT del.ContactID, GetDate(), 'Forename', del.Forename, ins.Forename, del.LastHistoryUserID
FROM deleted del inner join inserted ins on del.ContactID = ins.ContactID
end
IF Update(Surname)
BEGIN
INSERT INTO tContactHistory (ContactID, UpdateDate, ColumnName, oldvalue, newvalue, UserID)
SELECT del.ContactID, GetDate(), 'Surname', del.Surname, ins.Surname, del.LastHistoryUserID
FROM deleted del inner join inserted ins on del.ContactID = ins.ContactID
end
Cheers
I have a table where all the columns are updated even if there is no change to some of the columns from an SP
Now I have the following trigger but because I update all the columns it runs can I check the values are different then run it if they are?
ALTER TRIGGER TriggerContacts
ON dbo.tContacts
FOR update AS
IF Update(Forename)
BEGIN
INSERT INTO tContactHistory (ContactID, UpdateDate, ColumnName, oldvalue, newvalue, UserID)
SELECT del.ContactID, GetDate(), 'Forename', del.Forename, ins.Forename, del.LastHistoryUserID
FROM deleted del inner join inserted ins on del.ContactID = ins.ContactID
end
IF Update(Surname)
BEGIN
INSERT INTO tContactHistory (ContactID, UpdateDate, ColumnName, oldvalue, newvalue, UserID)
SELECT del.ContactID, GetDate(), 'Surname', del.Surname, ins.Surname, del.LastHistoryUserID
FROM deleted del inner join inserted ins on del.ContactID = ins.ContactID
end
Cheers