Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server CASE WHEN and IN construction

Having a problem with a WHERE search statement would like to use a construction like..

WHERE f.foo IN 
   CASE @bar
      WHEN 'BAR' THEN 
         ('FOO','BAR',BAZ')
      WHEN 'BAZ' THEN
         ('FOOBAR','FOOBAZ')
   END

or

WHERE CASE @bar
      WHEN 'BAR' THEN 
         f.foo IN ('FOO','BAR',BAZ')
      WHEN 'BAZ' THEN
         f.foo IN ('FOOBAR','FOOBAZ')
   END

where @bar is a well defined temp variable of the correct type and all that f is defined nicely..

I get an error about "Error at ','

like image 979
Jim Ward Avatar asked Dec 14 '22 00:12

Jim Ward


1 Answers

WHERE (@bar = 'BAR' and f.foo IN ('FOO', 'BAR', 'BAZ')) OR
      (@bar = 'BAZ' and f.foo IN ('FOOBAR', 'FOOBAZ'))
like image 95
Adam Robinson Avatar answered Dec 15 '22 14:12

Adam Robinson