I need to create a query which looks like this:
SELECT attr
FROM SomeTable B
WHERE EXISTS
(CASE WHEN B.some_attr = 0
THEN (SELECT x FROM C WHERE B.z = z)
WHEN B.some_attr = 1
THEN (SELECT x FROM D WHERE B.z = x)
END)
But SQL Server refuses to execute this bringing some syntax error.
How can I modify this query to make it work?
EDIT: I’m adding the error:
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword ‘CASE’.
Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword ‘WHEN’.
Msg 102, Level 15, State 1, Line 8 Incorrect syntax near ‘)’.
Try this: