Solved

SQL Server Optional Parameters

Posted on 2014-09-11
1
180 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

11 Experts available now in Live!

Get 1:1 Help Now