August 27, 2025 at 12:00 am
Comments posted to this topic are about the item Detecting Multiple Changes
August 27, 2025 at 6:48 am
If I want to check that ONLY the CustomerContactFirstName and CustomerContactLastName are update, then the right command is
IF COLUMNS_UPDATED ()= 0x0C0000
If I want to check that the CustomerContactFirstName and CustomerContactLastName are update together, then the right command is
SUBSTRING(COLUMNS_UPDATED (),1,1) & 12) = 12
update CustomerLarge set
CustomerContactFirstName= 1
,CustomerContactLastName= 1
August 27, 2025 at 7:45 am
Is it just me, or is this just a little bit, well, crap?
August 27, 2025 at 8:26 am
I doubt that anybody (besides some old, long retired boomer nerds that implented it decades ago) are using COLUMNS_UPDATED(), particularly since
Keep in mind that if UPDATE() or COLUMNS_UPDATED() returns "true", it does not mean, that there is really a change on the data in the column. It is just an indicator, that the column was part of the SET operator in the triggering UPDATE / MERGE.
You still need to compare the INSERTED / DELETED "tables" to find out, if there was really a change (imagine someone clicked onto the save button on a form and the app is not very sophicated and just runs an UPDATE on all columns that have an editable field on the screen, regardless if there was really a change or not)
God is real, unless declared integer.
August 27, 2025 at 12:26 pm
The question specifically states "I want to determine if both the CustomerContactFirstName and CustomerContactLastName fields are changed, but no others. "
That means that the first answer cannot be correct because it only checks for the first byte and none of the other bytes, which could have been changed.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 27, 2025 at 12:31 pm
I doubt that anybody (besides some old, long retired boomer nerds that implented it decades ago) are using COLUMNS_UPDATED(), particularly since
- you could write UPDATE(col1) OR UPDATE(col2) OR UPDATE(col3) to test multiple columns (or use AND if all needes to be part of the UPDATE/MERGE)
- you never know 100% exact if the columns are still in the same order 5 years later (and you really don't want to select sys.columns every time the trigger fires just to recalculate the bit value
- it is very clumsy when you have more then 8 columns in the table (=almost every table)
Keep in mind that if UPDATE() or COLUMNS_UPDATED() returns "true", it does not mean, that there is really a change on the data in the column. It is just an indicator, that the column was part of the SET operator in the triggering UPDATE / MERGE.
You still need to compare the INSERTED / DELETED "tables" to find out, if there was really a change (imagine someone clicked onto the save button on a form and the app is not very sophicated and just runs an UPDATE on all columns that have an editable field on the screen, regardless if there was really a change or not)
A million likes! Especially the part about how to detect actual data changes instead of some API writing the same value.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply