开发者

IN statement in result expression in CASE THEN expression

开发者 https://www.devze.com 2022-12-21 22:00 出处:网络
Is it possible to use IN in CASE..THEN ? WHERE record.field = CASE WHEN @flag = 1 THEN a WHEN @flag = 2 THEN IN (b, c)

Is it possible to use IN in CASE..THEN ?

WHERE
   record.field =
      CASE
          WHEN @flag = 1 THEN a
          WHEN @flag = 2 THEN IN (b, c)
      END

Or how to rew开发者_开发知识库rite such condition?


Sorry, no:

WHERE
    (@flag = 1 AND record.field = a)
    OR
    (@flag = 2 AND record.field IN (b, c))


try:

WHERE (@flag = 1 AND record.field = a )
    OR (@flag = 2 AND record.field IN (b, c))

it might be better to try to JOIN in the value and use an index:

DECLARE @Table (ValueOf  int)
INSERT INTO @Table ((ValueOf)
    SELECT a WHERE @flag = 1
    UNION SELECT b WHERE @flag = 2
    UNION SELECT c WHERE @flag = 2

SELECT
    ....
    FROM ...           x
    INNER JOIN @Table  t ON x...=t.ValueOf  
0

精彩评论

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