我想在CASE语句的部分中使用IN Clause。这给了我一些错误。
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
答案 0 :(得分:4)
为什么不使用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, ','))
)