Solved

T-SQL: modifying where clause depending on the sproc parameter values

Posted on 2010-11-10
5
668 Views
Last Modified: 2012-05-10
Depending on the sproc parameters value I have to add (or not) additional criteria’s to the where clause of the sproc select statement.
What is the best way to accomplish this?
Thanks.      

0
Comment
Question by:quasar_ee
5 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 34104976
Depending on what you need to do:

--use the parameter to include a column condition
select * from [tablename]
where cColumn = (case when @param=1 THEN somevalue ELSE cColumn END)

--use parameter as a column condition when it is not null
select * from [tablename]
where (@param IS NULL OR @param=cColumn)
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34104983
If you give your parameters a default value of null you can use the ISNULL or COALESCE functions in your WHERE clause.

Ex:

Create Procedure myproc
@Value as varchar = null
AS
Select *
from table
where value = ISNULL(@Value, value)

This basically will use the column if the @Value parameter is null.
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 34106529
0
 
LVL 6

Expert Comment

by:dan_mason
ID: 34111059
Shaun Kline's info is good, however for large datasets the query is likely to be more efficient if instead of:

WHERE value= COALESCE(@Value,value)

you use:

WHERE value LIKE COALESCE(@Value,'%')

That may sound counter-intuitive since 'LIKE' is usually much slower than =, but in the case of an optional parameter I've found it works quicker.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34111379
I have found that using
where (@param IS NULL OR @param=cColumn)
out performs ISNULL or COALESCE
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question