sql case语句在哪里?

时间:2017-07-07 03:20:27

标签: sql sql-server-2008 case

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或Status Ind = 0

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

您的CASE语句需要修改

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

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