Have query like :
declare @case int = 1
SELECT a.column1,b.column2
FROM tbl1 a
INNER JOIN tbl2 b on a.id = b.id and a.category in(1,2,3,4,5)
Need when @case = 1
category must be in (1,2,3,4,5), when @case = 2
it must be (1,2,4,5). Something like this :
SELECT a.column1,b.column2
FROM tbl1 a
INNER JOIN tbl2 b on a.id = b.id and (CASE WHEN @case = 1 THEN a.category in(1,2,3,4,5) WHEN @case = 2 THEN (1,2,4,5) ELSE '')
How to do it ?
SELECT a.column1,b.column2
FROM tbl1 a
INNER JOIN tbl2 b on a.id = b.id
and
(
(@case = 1 and a.category in (1,2,3,4,5)) OR
(@case = 2 and a.category in (1,2,4,5))
)
I added parentheses for readability. Not all of them are necessary.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments