Assume that I have 2 tables named aTable1, aTable2
aTable1 has userID set to identity and contains the following data:
userID email FirstName LastName
1 NULL C CC
2 NULL D DD
3 a@yahoo.com A AA
4 b@yahoo.com B BB
5 e@yahoo.com E EE
6 f@yahoo.com NULL NULL
7 g@yahoo.com NULL NULL
aTable2 contains the following data:
userID email FirstName LastName Title
3 a@yahoo.com A AA student
4 b@yahoo.com B BB student
5 e@yahoo.com E EE student
NULL NULL C CC dean
NULL NULL D DD advisor
NULL f@yahoo.com NULL NULL student2
NULL g@yahoo.com NULL NULL student3
I want to update aTable2.userID based on aTable1, but knowing that 2 tables have null values in it, so i do like this:
set ANSI_NULLS off
update aTable2
set aTable2.userID = a.userID
from aTable a, aTable2 b
where a.FirstName = b.FirstName and a.LastName = b.LastName and a.email = b.email
However, what this update does not update all the userID, in fact, it only updates those that have email not equal to null, but I already set ANSI_NULLS to off.
What did I do wrong?
To get your update query to work, you can try something like this:
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:
Good luck.