Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-10
5
Medium Priority
?
699 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 2000 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 27

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

971 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