Solved

Show All if Param is null

Posted on 2014-04-29
4
287 Views
Last Modified: 2014-04-29
Dear Experts,

I have this SP filter however what I would like to happen is when @dept is null, I want to show all department, can anyone help me to make it happen?

"  HAVING     dbo.CostCenter.DeptID = ISNULL(@dept, 1030) AND
  (dbo.WIR.IssuedDate BETWEEN CONVERT(DATETIME, @sdat + '00:00:00', 102) AND CONVERT(DATETIME, @fdat + '23:59:59', 102))  "


Thanks.
0
Comment
Question by:JimiJ13
4 Comments
 
LVL 8

Expert Comment

by:ProjectChampion
ID: 40029218
If I understand correctly you want to make the @dept an optional parameter so that when the proc is called with a value for @dept, filters the result by that dept# and if not (i.e. null value passed on to the param then returns all departments in the result?

In that case all you need to do is to change the signature of your stored proc, and replace
"@dept <datatype>" to "@dept <datatype> = NULL". This makes the @dept an optional parameter, so a caller can omit it if they want, in which case it'll get null value by default; or the caller can include the parameter and pass null value explicitly (which will have the same effect as the previous case); or as they currently do, pass a not null value as parameter.

You also need to change the logic of the proc and introduce a conditional branching, so that base on whether or not @dept is not null, either return the result as it does now or return it for all departments by changing the HAVING clause to:
"  HAVING   (dbo.WIR.IssuedDate BETWEEN CONVERT(DATETIME, @sdat + '00:00:00', 102) AND CONVERT(DATETIME, @fdat + '23:59:59', 102))  ".
0
 
LVL 8

Accepted Solution

by:
Ganapathi earned 500 total points
ID: 40029243
If you pass a valid value(NOT NULL) for department, then it will use it. else it will ignore the where and fetch all the data from the Table.

WHERE
   1 = (CASE
           WHEN @Dept IS NULL
              THEN 1
           ELSE 2
        END)
  OR tbl.Dept = @DeptParam

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40029313
As indicated previously this condition should be in a WHERE clause and not in a HAVING clause.
0
 

Author Closing Comment

by:JimiJ13
ID: 40029369
It works and easy to follow.  Great!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

746 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