Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using multiple parameter values in where statement 1

Status
Not open for further replies.

Luvsql

Technical User
Apr 3, 2003
1,179
CA
I have a parameter @Quotes which will prompt the user for Yes or No. If @Quotes='Yes' then I need the where statement to only include field OrderType='QUotes' but if @Quotes='No' then it is to include OrderType='Quotes' and OrderType='Orders'

How do you incorporate an If or case syntax within the where statement of a stored procedure, without having to pass the IF first (so there's 2 queries on the data)?
 
Something like this:

Code:
Where (@Quotes = 'Yes' And OrderType = 'Quotes')
      Or
      (@Quotes = 'No' And OrderType in ('Quotes','Orders'))

Note that this where clause is not sargable, so performance may not be great, but it's worth a try.


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
you might get better performance if you use this
Code:
Select ....
from tablename
where OrderType = 'Quotes'
union
Select ....
from tablename
where OrderType = 'Orders'
And @Quotes = 'No'
 
Good point PWise, but I think UNION ALL would be faster still.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I didn't even think about that logic and it works perfectly!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top