【问题标题】:sql case statement in where?sql case语句在哪里?
【发布时间】:2017-07-07 03:20:26
【问题描述】:
select t1.customer_code as doc_num, CONVERT(VARCHAR,t2.created_on,103) as doc_date,
t2.sap_cardcode as sap_doc_num ,t2.void_flg,t2.status_ind,t2.err_msg
from customer t1
inner join sap_customer t2 on t1.id = t2.customer_id
where t2.status_ind = case when @test = 'todo' then t2.status_ind='1' else t2.status_ind='0' END

上面是我需要做的 select 语句,基于 where 语句我需要传递一个参数来确定它应该执行哪个想要。 状态 Ind = 1 或状态 Ind = 0

【问题讨论】:

  • 您遇到的错误是什么?
  • "=" 附近的语法不正确
  • 在 WHERE 子句中使用 AND/OR 而不是 case 表达式通常会更好。

标签: sql sql-server-2008 case


【解决方案1】:

我在您的查询中稍微修改了案例部分。希望这会有所帮助:

select t1.customer_code as doc_num, CONVERT(VARCHAR,t2.created_on,103) as doc_date,t2.sap_cardcode as sap_doc_num ,t2.void_flg,t2.status_ind,t2.err_msg
from customer t1
inner join sap_customer t2 on t1.id = t2.customer_id
where t2.status_ind = case when @test = 'todo' then '1' else '0' END

或者你可以使用下面的例子:

where t2.status_ind = case @test when 'todo' then '1' else '0' END

【讨论】:

    【解决方案2】:

    您的CASE 语句需要修改

    case when @test = 'todo' then '1' else '0' END
    

    因为,你要比较where t2.status_ind。因此,您需要从 CASE 返回一些值,而不是设置它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      相关资源
      最近更新 更多