简体   繁体   中英

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 rewrite 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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM