Solved

Show All if Param is null

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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