SQL Server 中的 OR 运算符短路 [英] OR Operator Short-circuit in SQL Server

查看:118
本文介绍了SQL Server 中的 OR 运算符短路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想咨询SQL Server OR短路

I want to consult SQL Server OR short-circuit

代码:

   DECLARE @tempTable table
      (
         id int
      )
      INSERT @tempTable(id) values(1)

      DECLARE @id varchar(10)
      SET @id = 'x'
      SELECT * FROM @tempTable WHERE 1=1 OR id = @id --successfully
      SELECT * FROM @tempTable WHERE @id = 'x' OR id = @id --Exception not Convert 'x' to int

为什么?1=1 并且@id='x' 为真.

Why? 1=1 and @id='x' is true.

SQL Server OR 运算符:是否有短路功能?

SQL Server OR operator : whether the short-circuit function?

谢谢

推荐答案

在 SQL 中,不要求 OR 子句提前中断.换句话说,是否同时检查两个条件取决于优化器.我不是 MSSQL 优化器的专家,但我见过优化器有或没有短路 OR 子句的实例.

Within SQL, there is no requirement that an OR clause breaks early. In other words, it is up to the optimizer whether to check both conditions simutaneously. I am not an expert in the MSSQL optimizer, but I have seen instances where the optimizer has and has not short circuited an OR clause.

这篇关于SQL Server 中的 OR 运算符短路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆