Not*_*eet 17 t-sql sql-server query-optimization short-circuiting
我想咨询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
Run Code Online (Sandbox Code Playgroud)
为什么?1 = 1且@ id ='x'为真.
SQL Server OR运算符:是否有短路功能?
谢谢
只是偶然发现了这个问题,并且已经找到了这个博客条目:http://rusanu.com/2009/09/13/on-sql-server-boolean-operator-short-circuit/
SQL服务器可以在任何它认为合适的地方自由地优化查询,因此在博客文章中给出的示例中,您不能依赖于短路.
但是,CASE显然需要记录以书面顺序进行评估 - 检查该博客文章的评论.