2017-07-07 177 views
0
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语句,我需要传递一个param来确定它应该执行哪个。 Status Ind = 1或Status Ind = 0sql case语句在哪里?

+0

你得到的错误是什么? – Ravi

+0

“=”附近的语法不正确 – hunt

+0

在WHERE子句中使用AND/OR而不是用例表达式通常更好。 – jarlh

回答

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 
2

CASE语句需要修改

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

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