有没有办法在 IN() 语句中嵌套 CASE-WHEN 语句,以便 WHEN 或 ELSE 之一返回子查询。对我来说,这不应该是一个问题,但不知何故我得到了错误:
“子查询返回超过 1 个值。”
IN()
应该处理多个值!
这是一个重现错误的小示例:
-- tblA will be searched for values
Declare @tblA table (i int)
insert @tblA
select 1
union select 2
union select 3
--tblB: its values will be searched in tblA
Declare @tblB table (i int)
insert @tblB
select 2
union select 3
union select 1
--@c used by the CASE statement to match
declare @c varchar(50)
set @c = 'Match'
select *
from @tblA
where i IN ( -- IN statement should accept subquery in it
case @c
when 'Dont Match' then 2 --If it had matched, then the single value 2 would have been returned and TSQL would be happy
else (
select i from @tblB --TSQL not happy and causing error when more than one values returned from the subquery
)
end
)