Why does NULL = NULL evaluate to false in SQL server

前端 未结 19 1538
一向
一向 2020-11-22 05:02

In SQL server if you have nullParam=NULL in a where clause, it always evaluates to false. This is counterintuitive and has caused me many errors. I do understa

相关标签:
19条回答
  • 2020-11-22 05:34

    MSDN has a nice descriptive article on nulls and the three state logic that they engender.

    In short, the SQL92 spec defines NULL as unknown, and NULL used in the following operators causes unexpected results for the uninitiated:

    = operator NULL   true   false 
    NULL       NULL   NULL   NULL
    true       NULL   true   false
    false      NULL   false  true
    
    and op     NULL   true   false 
    NULL       NULL   NULL   false
    true       NULL   true   false
    false      false  false  false
    
    or op      NULL   true   false 
    NULL       NULL   true   NULL
    true       true   true   true
    false      NULL   true   false
    
    0 讨论(0)
  • 2020-11-22 05:37

    Think of the null as "unknown" in that case (or "does not exist"). In either of those cases, you can't say that they are equal, because you don't know the value of either of them. So, null=null evaluates to not true (false or null, depending on your system), because you don't know the values to say that they ARE equal. This behavior is defined in the ANSI SQL-92 standard.

    EDIT: This depends on your ansi_nulls setting. if you have ANSI_NULLS off, this WILL evaluate to true. Run the following code for an example...

    set ansi_nulls off
    
    if null = null
        print 'true'
    else
        print 'false'
    
    
    set ansi_nulls ON
    
    if null = null
        print 'true'
    else
        print 'false'
    
    0 讨论(0)
  • 2020-11-22 05:37

    If you are looking for an expression returning true for two NULLs you can use:

    SELECT 1 
    WHERE EXISTS (
        SELECT NULL
        INTERSECT
        SELECT NULL
    )
    

    It is helpful if you want to replicate data from one table to another.

    0 讨论(0)
  • 2020-11-22 05:41

    Just an addition to other wonderful answers:

    AND: The result of true and unknown is unknown, false and unknown is false,
    while unknown and unknown is unknown.
    
    OR: The result of true or unknown is true, false or unknown is unknown, while unknown or unknown is unknown.
    
    NOT: The result of not unknown is unknown
    
    0 讨论(0)
  • 2020-11-22 05:42

    To quote the Christmas analogy again:

    In SQL, NULL basically means "closed box" (unknown). So, the result of comparing two closed boxes will also be unknown (null).

    I understand, for a developer, this is counter-intuitive, because in programming languages, often NULL rather means "empty box" (known). And comparing two empty boxes will naturally yield true / equal.

    This is why JavaScript for example distinguishes between null and undefined.

    0 讨论(0)
  • 2020-11-22 05:43

    NULL isn't equal to anything, not even itself. My personal solution to understanding the behavior of NULL is to avoid using it as much as possible :).

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