开发者

IN statement in result expression in CASE THEN expression

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜