?
Solved

SQL Server Optional Parameters

Posted on 2014-09-11
1
Medium Priority
?
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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