?
Solved

Show All if Param is null

Posted on 2014-04-29
4
Medium Priority
?
299 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
[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
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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

777 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