TSql Trigger needs to fire only on columns whose values have changed
sql-server, t-sql, triggers
Solution
IF EXISTS (SELECT *
FROM
INSERTED I JOIN DELETED D ON I.Key = D.Key
WHERE
I.Col <> D.Col)
...
or use a table variable to cache thus to avoid repeated use of I and D.
SELECT
CASE WHEN I.Col1 <> D.Col1 THEN 1 ELSE 0 END AS Col1Diff,
CASE WHEN I.Col2 <> D.Col2 THEN 1 ELSE 0 END AS Col2Diff,
...
FROM
INSERTED I JOIN DELETED D ON I.Key = D.Key
or combine ideas to test all changes up front and exit the trigger
Problem
I wrote a trigger that needs to do some different work on a table based on which columns in a row actually updated. I accomplished this using ``` IF UPDATE(column-name) ``` That part works fine. It turns out, however, that there are other parts of the code that update rows by setting every single value whether the value actually changed or not and this causes the trigger to fire for parts that were "updated" but whose values did not actually change at all. As changing the code that's causing this is probably not an option, is there an easier way to prevent this other than having to compare between the INSERTED and DELETED tables (in which case the IF UPDATEs are meaningless)?