views:

53

answers:

4

I have this IF statement in my Stored Procedure:

if ((@someParam = 'C') OR (@someParam = 'I' AND @SomeOtherParam <> 2 AND @SomeOtherParam <> 4 AND @SomeOtherParam <> 5))

My question is can I check @SomeOtherParam in one shot, instead of having to check it 3 separate times?

+6  A: 

This should do the trick:

if ((@someParam = 'C') OR (@someParam = 'I' AND @SomeOtherParam NOT IN (2,4,5))) 

IN takes a list of values, and returns true if your value is found in the list. Adding NOT means that it will return true if your value is not found.

Michael Madsen
+1  A: 

maybe something with CASE statements:

if case @someparam when 'C' then 1 when 'I' then @someotherparam NOT IN (2,4,5) else 0 end
Benoit
+1  A: 

Try

if (@SomeOtherParam  NOT IN (2, 4, 5))
Noel Abrahams
A: 

There is also EXISTS keyword in SQL which you can use

if not EXISTS (select * from list where my_column= @someotherparam )
adopilot