在where语句中使用(case和len)时出错



我正在尝试编写一个存储过程,以便根据@acc_id的值从其中一列中提取数据。

正如你所看到的,我在where CASE (len(acc_id) > 8 then client_id ELSE client_acc end)上遇到了一个错误

但我不确定我做错了什么。如有任何协助,我将不胜感激。

create procedure test_client @acc_id nvarchar(20)
as
Select 
client_id,
cash_t.client_acc,
sub_trans_type,
settle_date,
dbt_crt,
cash_t.currency,
cash_amount,
cash_t.last_update
From test_cash_trans as cash_t
inner join client_crm as crm
on cash_t.client_acc = crm.client_acc
where crm.client_acc in (
select  client_acc
from client_crm
where CASE (len(acc_id) > 8 then client_id ELSE client_acc end) 
)
order by crm.client_acc ASC
GO

结果:

11:31:47在第104行开始执行查询Msg 102,Level 15,State1,过程test_client,第20行">"附近的语法不正确。全部的执行时间:00:00:00.010

您需要一个不同的WHERE子句。这种误解的原因可能是,在T-SQL中,CASE是一个表达式,而不是";"流量控制";语言元素。

WHERE 
(
(LEN(@acc_id) > 8) AND (crm.client_acc in (SELECT client_id FROM client_crm))
) OR
(
(LEN(@acc_id) <= 8) AND (crm.client_acc in (SELECT client_acc FROM client_crm))
)

最新更新