Solved

SQL Server Optional Parameters

Posted on 2014-09-11
1
182 Views
Last Modified: 2014-09-11
I am writing a stored proc for an SSRS report.  The user wants the report to bring back all the data on the first run and then allow the user to set 4 parameters.  I initially created the report to show nothing until the user selected parameters, but they want all data returned and then filter it out.  

I have four parameters  @LiveDate datetime = Null, @Status bit = Null,@CarType varchar(30) = NULL,@somethingelse varchar(30) = NULL

Then I have a select statement.  It the WHERE clause that I am confused about.  If the parameters are NULL, then I want to return everything  and if not then return according to the parameter values.

Can someone help me with the WHERE clause here.  

Thanks.
0
Comment
Question by:sherbug1015
1 Comment
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40317903
You can use the ISNULL function to handle this:
WHERE <Your Field> = ISNULL(@YourParam, <Your Field>)

Another method is:
WHERE (@YourParam IS NULL OR <Your Field> = @YourParam)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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.
In this article I will describe the Backup & Restore 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 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…

856 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