在 where 语句中使用 (case 和 len) 时出错
Error when using (case and len) in where statement
我正在尝试编写一个存储过程以根据 @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 Started executing query at Line 104 Msg 102, Level 15, State
1, Procedure test_client, Line 20 Incorrect syntax near '>'. Total
execution time: 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))
)
我正在尝试编写一个存储过程以根据 @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 Started executing query at Line 104 Msg 102, Level 15, State 1, Procedure test_client, Line 20 Incorrect syntax near '>'. Total execution time: 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))
)