Solved

Show All if Param is null

Posted on 2014-04-29
4
296 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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
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.

763 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