我想咨询SQL Server OR短路
代码:
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'
为 true
。
SQL Server OR运算符:是否有短路功能?
谢谢
最佳答案
在 SQL 中,不要求 OR 子句提前中断。换句话说,由优化器决定是否同时检查这两个条件。我不是 MSSQL 优化器方面的专家,但我见过优化器有或没有短路 OR 子句的实例。
关于sql-server - SQL Server 中的 OR 运算符短路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11219791/