开发者

IN inside a case statement of WHERE clause

开发者 https://www.devze.com 2023-03-27 18:23 出处:网络
Is it possible to add IN inside a case statement of WHERE clause as below: WHERE CASE WHEN @id IN(1,2) THEN ...

Is it possible to add IN inside a case statement of WHERE clause as below:

WHERE

CASE WHEN @id IN(1,2) THEN ...
CASE WHEN @id IN(3,4) THEN...

I got syntax error...I then tried by removing IN adding OR as below:

开发者_C百科
CASE WHEN @id = 1 OR @id = 2 THEN ...
CASE WHEN @id = 3 OR @id = 4 THEN ...

It worked....but just for curiosity I want to know whether somehow we can use IN... please help


You need to explain further. This is definitely valid syntax:

DECLARE @id int = 1

SELECT 'Foo'
WHERE 1 = CASE WHEN @id IN (1,3) THEN 1
          ELSE 0 
          END

What is in your THEN statement?


You might try to put the condition in parenthesis, like:

( @id IN(1,2) )
0

精彩评论

暂无评论...
验证码 换一张
取 消