Is the SQL WHERE clause short-circuit evaluated?

前端 未结 14 2735
时光取名叫无心
时光取名叫无心 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 05:02

    From the above, short circuiting is not really available.

    If you need it, I suggest a Case statement:

    Where Case when Expr1 then Expr2 else Expr3 end = desiredResult
    

    Expr1is always evaluated, but only one of Expr2 and Expr3 will be evaluated per row.

提交回复
热议问题