Solved

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

Posted on 2010-11-10
5
641 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now