Is the SQL WHERE clause short-circuit evaluated?

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

    I typically use this for optional parameters. Is this the same as short circuiting?

    SELECT  [blah]
    FROM    Emp
    WHERE  ((@EmpID = -1) OR (@EmpID = EmpID))
    

    This gives me the option to pass in -1 or whatever to account for optional checking of an attribute. Sometimes this involves joining on multiple tables, or preferably a view.

    Very handy, not entirely sure of the extra work that it gives to the db engine.

提交回复
热议问题