Is the SQL WHERE clause short-circuit evaluated?

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

    Just stumbled over this question, and had already found this blog-entry: http://rusanu.com/2009/09/13/on-sql-server-boolean-operator-short-circuit/

    The SQL server is free to optimize a query anywhere she sees fit, so in the example given in the blog post, you cannot rely on short-circuiting.

    However, a CASE is apparently documented to evaluate in the written order - check the comments of that blog post.

提交回复
热议问题