Why Ansi_nulls does not work?

前端 未结 2 913
时光取名叫无心
时光取名叫无心 2020-12-22 05:36

Assume that I have 2 tables named aTable1, aTable2

aTable1 has userID set to identity and contains the following data:

userID  email          FirstNa         


        
相关标签:
2条回答
  • 2020-12-22 05:51

    It is documented that it won't work:

    SET ANSI_NULLS ON affects a comparison only if one of the operands of the comparison is either a variable that is NULL or a literal NULL. If both sides of the comparison are columns or compound expressions, the setting does not affect the comparison.

    0 讨论(0)
  • 2020-12-22 06:00

    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.

    0 讨论(0)
提交回复
热议问题