3

Why is the following syntax generates the error "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
4

2 回答 2

3

您不能让查询的某些部分是动态的。它必须是全部或全部。然后使用EXEC()运行您的动态查询

exec('select order_status.order_id
      from order_status
      where ' + @orderwhere)
于 2013-07-22T11:47:25.727 回答
1

您的 where 子句是一个字符串。您应该使用动态 SQL 或类似的东西:

use orders
declare @orderwhere varchar(5000)
set @orderwhere = '1'
select order_status.order_id
from order_status
where order_status.step = @orderwhere
于 2013-07-22T11:49:09.603 回答