Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assistance with stored procedure setting a blank variable to retrieve all

Posted on 2008-10-28
3
Medium Priority
?
173 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
[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
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 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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