Solved

Assistance with stored procedure setting a blank variable to retrieve all

Posted on 2008-10-28
3
167 Views
Last Modified: 2010-04-21
I have a stored procedure with variables being sent to it such as dates, facility, employees, which are used in the final SELECT statement (See snippet). Sometimes an Employee or Facility is not given, because we want to see all employees at a given Facility.

I have tried setting the value of the variable @Employee = '%' but it will not return all. How can I get it to show all if the value of this variable is left blank?

Thanks
ALTER   PROC [dbo].[spNC_BookingWizard]
	@StartDate varchar(10) = '1/1/1900',
	@EndDate varchar(10) = '1/1/2010',
	@Employee varchar(200) = '',
	@Facility varchar(200) = '',
	@Status varchar(200) = '',
	@test bit = 0
AS
 
SET NOCOUNT ON
 
DECLARE @dtStart datetime
DECLARE @dtEnd datetime
 
 
SET @dtStart = CONVERT(datetime, @StartDate)
SET @dtEnd = DATEADD(dd, 1, DATEADD(dd, 0, CONVERT(datetime, @EndDate)))
 
...........
 
SELECT * FROM #tmpBook 
WHERE StartDate_date >= @dtStart AND StartDate_date <= @dtEnd 
AND Facility = @Facility
AND EmpName = @Employee
AND  BookingStatus = @Status

Open in new window

0
Comment
Question by:NursingCorp
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22827351
if the column value is NULL, you would have to make the condition WHERE yourcol IS NULL.

for you, this syntax should work:
SELECT * FROM #tmpBook 
WHERE StartDate_date >= @dtStart AND StartDate_date <= @dtEnd 
AND ( Facility = @Facility OR @Facility = '%' )
AND ( EmpName = @Employee OR @Employee = '%' )
AND  BookingStatus = @Status

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22827372
angelIII is absolutely correct that it should be NULL for your default parameter.  Here's how I do this sort of thing (optional parameters)
ALTER   PROC [dbo].[spNC_BookingWizard]
        @StartDate datetime = null,
        @EndDate datetime = null,
        @Employee varchar(200) = null,
        @Facility varchar(200) = null,
        @Status varchar(200) = null,
        @test bit = 0
AS
 
SET NOCOUNT ON
 
...........
 
SELECT * FROM #tmpBook 
WHERE (@StartDate is null  or StartDate_date >= @StartDate)
  AND (@EndDate is null    or StartDate_date <= @EndDate)
  AND (@Facility is null   or Facility = @Facility)
  AND (@employee is null   or EmpName = @Employee)
  AND (@Status is null     or BookingStatus = @Status)

Open in new window

0
 

Author Closing Comment

by:NursingCorp
ID: 31510994
Copied and pasted...worked perfect. Thanks
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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