Is the SQL WHERE clause short-circuit evaluated?

前端 未结 14 2661
时光取名叫无心
时光取名叫无心 2020-11-22 04:31

Are boolean expressions in SQL WHERE clauses short-circuit evaluated ?

For example:

SELECT * 
FROM Table t 
WHERE @key IS NULL OR (@key IS NOT NULL          


        
14条回答
  •  甜味超标
    2020-11-22 04:58

    I think this is one of the cases where I'd write it as if it didn't short-circuit, for three reasons.

    1. Because for MSSQL, it's not resolved by looking at BOL in the obvious place, so for me, that makes it canonically ambiguous.

    2. because at least then I know my code will work. And more importantly, so will those who come after me, so I'm not setting them up to worry through the same question over and over again.

    3. I write often enough for several DBMS products, and I don't want to have to remember the differences if I can work around them easily.

提交回复
热议问题