views:

45

answers:

2

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?

+4  A: 

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  
KM
+4  A: 

Sorry, no:

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