Solved

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

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql2016-WIn10: standard,for SQL servc-account.. 51 42
SQL Recursion 6 33
SQL Server 2008 R2 service pack updates 5 42
how to make geography query faster?  SQL 7 40
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.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 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