I'm working with the following query:
SELECT *
FROM TableA a
WHERE a.FieldA IN (
CASE
--select subquery returns a single value
WHEN a.FieldB = 'Value1'
THEN (select b.ID from TableB b where b.FK_Field = '123')
--select subquery returns multiple values
WHEN a.FieldB = 'Value2'
THEN (select c.ID from TableC c where c.FK_Field = '123')
END
)
The first case select statement returns only a single b.ID
. If I just have that statement, my code works.
The second case statement, however, returns multiple c.ID
s. When I add that check, I get the following error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
If I would have WHERE a.FieldA =
, then I understand that the subquery can only return 1 value. I however have WHERE a.FieldA IN
, so why is it complaining if there are multiple values returned?
How can I implement this kind of check?