【问题标题】: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。然后你必须exec()

标签: 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 2011-09-22
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多