【问题标题】:An expression of non-boolean type specified in a context where a condition is expected, near '@orderwhere'在预期条件的上下文中指定的非布尔类型表达式,靠近“@orderwhere”
【发布时间】:2013-07-22 11:41:55
【问题描述】:
为什么以下语法会生成错误“An expression of non-boolean type specified in a context where a condition is expected, near '@orderwhere'”
use orders
declare @orderwhere varchar(5000)
set @orderwhere = 'order_status.step = 1'
select order_status.order_id
from order_status
where @orderwhere
【问题讨论】:
标签:
sql
sql-server
syntax
【解决方案1】:
您的部分查询不能是动态的。它必须是全部或全部。然后使用EXEC() 运行您的动态查询
exec('select order_status.order_id
from order_status
where ' + @orderwhere)
【解决方案2】:
你的 where 子句是一个字符串。您应该使用动态 SQL 或类似的东西:
use orders
declare @orderwhere varchar(5000)
set @orderwhere = '1'
select order_status.order_id
from order_status
where order_status.step = @orderwhere