Assume that I have 2 tables named aTable1, aTable2
aTable1 has userID set to identity and contains the following data:
userID email FirstNa
It is documented that it won't work:
SET ANSI_NULLS ONaffects a comparison only if one of the operands of the comparison is either a variable that isNULLor a literalNULL. If both sides of the comparison are columns or compound expressions, the setting does not affect the comparison.
To get your update query to work, you can try something like this:
UPDATE a2
SET
userId = a.UserId
FROM
aTable2 a2
JOIN aTable1 a ON
ISNULL(a.Email,'NULL') = ISNULL(a2.Email,'NULL') AND
ISNULL(a.FirstName,'NULL') = ISNULL(a2.FirstName,'NULL') AND
ISNULL(a.LastName,'NULL') = ISNULL(a2.LastName,'NULL')
When the values are NULL, I've arbitrarily set the value to 'NULL' -- use some distinct value that will not appear in your data to ensure you won't receive false positives.
I've also seen other solutions using OR criteria in the JOIN and checking both values for NULL:
((a.Email = a2.Email) OR (a.Email IS NULL AND a2.Email IS NULL)) ...
Good luck.