Sql server 如何在CASE语句的THEN部分使用IN子句而不出错?
我想在CASE语句的Then部分使用IN子句。这给了我一些错误Sql server 如何在CASE语句的THEN部分使用IN子句而不出错?,sql-server,Sql Server,我想在CASE语句的Then部分使用IN子句。这给了我一些错误 declare @Role varchar(10),@Region_Code varchar(100) set @Role='AB' SEt @Region_Code='003,002,004,005' select * from [tableName] BM where CASE WHEN @Role='AB' THEN BM.[Region code] in (Select * from dbo.Split (@Region_
declare @Role varchar(10),@Region_Code varchar(100)
set @Role='AB'
SEt @Region_Code='003,002,004,005'
select * from [tableName] BM
where CASE WHEN @Role='AB'
THEN BM.[Region code] in (Select * from dbo.Split (@Region_Code, ','))
WHEN @Role='XYZ'
THEN BM.Branch_code in (Select * from dbo.Split (@Region_Code, ','))
end
为什么不使用IF-ELSE-IF语句
IF @Role='AB'
select * from [tableName] BM
where BM.[Region code] in (Select RegionCode from dbo.Split (@Region_Code, ','))
ELSE IF @Role='XYZ'
select * from [tableName] BM
where BM.[Branch_code] in (Select RegionCode from dbo.Split (@Region_Code, ','))
也试试这个
select * from [tableName] BM
where ( @Role='AB' AND BM.[Region code] in (Select RegionCode from dbo.Split(@Region_Code, ',')) )
OR
( @Role='XYZ'
AND BM.Branch_code in (Select RegionCode from dbo.Split (@Region_Code, ','))
)
大小写是一个表达式,而不是一个流控件。你不能这样使用它。在你将错误添加到你的问题之前,我们需要问你错误是什么吗?