Solved

Show All if Param is null

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

707 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