Why does NULL = NULL evaluate to false in SQL server

前端 未结 19 1604
一向
一向 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
    

提交回复
热议问题