Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Stored procedure not returning correct data

Author  Topic 

Eyespi20
Starting Member

10 Posts

Posted - 2012-08-13 : 11:23:52
I have a stored procedure that accepts as input parameters, an id, a datet ime and returns a start date(with time), end date(with time) and availability based on querying a table on the id and the input date.

The table queried has 7 fields corresponding to the 7 days of the week, and a start time and end time for each day. So it's set up like this:
ID, WorkDay_1, Workday_2, workday_3, workday_4, workday_5, workday_6, workday_7, starttime_1, startTime_2, startTime_3, starttime_4, startTime_5, startTime_6, startTime_7, endTime_1, endTime_2, endTime_3, EndTime_4, EndTime_5, EndTime_6, Endtime_7.

The stored procedure grabs the day of week from the passed in date/time parameter then uses a case statement to pull the 1 or 0 from the corresponding workday field to determine if the person is on or off (1 being on, 0 being off) and then cobbles the time from the start time to the date passed in and the time from the end time field to the passed in date to determine if the person was 1. Available on the day and B. available during the time.

I have a person set to ON 24/7. When I run the stored procedure against this person, I get output of null for available and null for a start time but get a valid end time.

I've double and triple checked that the person has all days selected on and a start time of 00:00:00:00 and an end time of 23:59:59:59 (although this is an SQL 2008 R2 database, the tables came over from SQL 2000, so it isn't making use of the new data types and as this is a 3rd party proprietary software I'm reluctant to change the data types on the relevant tables for fear of breaking the software.)

If I copy the SP code, give it the same parameters, it returns the expected data. WHY does it not return the same when it's executed?

Margaret
    è¿é

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-13 : 11:32:53
From your description, it is hard to figure out what may be wrong. If you can post the code for the stored proc along with some sample data and expected output, that would help someone on the forum to respond. If the stored proc is long, if you can simulate a simpler case, that would help.

Brett's blog here might be of some help in posting: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Eyespi20
Starting Member

10 Posts

Posted - 2012-08-13 : 11:36:49
CODE /
ALTER Procedure [dbo].[SVC_Get_Tech_Work_Hours] (@TechID char(11), @InDate datetime, @Avail smallint OUTPUT, @STARTTIME datetime OUTPUT, @ENDTIME datetime OUTPUT) As declare @Day smallint select @Day = DATEPART(dw,@InDate) select @Avail = case @Day WHEN 2 THEN WORKDYS_1 WHEN 3 THEN WORKDYS_2 WHEN 4 THEN WORKDYS_3 WHEN 5 THEN WORKDYS_4 WHEN 6 THEN WORKDYS_5 WHEN 7 THEN WORKDYS_6 WHEN 1 THEN WORKDYS_7 END from SVC00100 WHERE TECHID = @TechID if @Avail <> 0 BEGIN select @STARTTIME = case @Day WHEN 2 THEN WORKSTRT_1 WHEN 3 THEN WORKSTRT_2 WHEN 4 THEN WORKSTRT_3 WHEN 5 THEN WORKSTRT_4 WHEN 6 THEN WORKSTRT_5 WHEN 7 THEN WORKSTRT_6 WHEN 1 THEN WORKSTRT_7 END, @ENDTIME = case @Day WHEN 2 THEN WORKEND_1 WHEN 3 THEN WORKEND_2 WHEN 4 THEN WORKEND_3 WHEN 5 THEN WORKEND_4 WHEN 6 THEN WORKEND_5 WHEN 7 THEN WORKEND_6 WHEN 1 THEN WORKEND_7 END from SVC00100 WHERE TECHID = @TechID select @STARTTIME = CONVERT(varchar(20),@InDate,102) + ' ' + CONVERT(varchar(20),@STARTTIME,108), @ENDTIME = CONVERT(varchar(20),@InDate,102) + ' ' + CONVERT(varchar(20),@ENDTIME,108) END ELSE select @STARTTIME = @InDate, @ENDTIME = @InDate return (0)
/CODE

Input values '77777', '08/10/2012 16:30:06'

Margaret
    è¿é
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-13 : 11:38:21
please format it properly and post

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-13 : 11:41:08
I just happen to have a formatting tool - so below is the formatted code. Eyespi, if you can post the parameters that you are providing when you call, that would help. Provide two sets of parameters - one for which it works correctly, and the second set for which it does not.
ALTER PROCEDURE [dbo].[SVC_Get_Tech_Work_Hours] (
@TechID CHAR(11),
@InDate DATETIME,
@Avail SMALLINT OUTPUT,
@STARTTIME DATETIME OUTPUT,
@ENDTIME DATETIME OUTPUT
)
AS
DECLARE @Day SMALLINT
SELECT
@Day = DATEPART(dw, @InDate)

SELECT
@Avail = CASE @Day
WHEN 2 THEN WORKDYS_1
WHEN 3 THEN WORKDYS_2
WHEN 4 THEN WORKDYS_3
WHEN 5 THEN WORKDYS_4
WHEN 6 THEN WORKDYS_5
WHEN 7 THEN WORKDYS_6
WHEN 1 THEN WORKDYS_7
END
FROM
SVC00100
WHERE
TECHID = @TechID

IF @Avail <> 0
BEGIN
SELECT
@STARTTIME = CASE @Day
WHEN 2 THEN WORKSTRT_1
WHEN 3 THEN WORKSTRT_2
WHEN 4 THEN WORKSTRT_3
WHEN 5 THEN WORKSTRT_4
WHEN 6 THEN WORKSTRT_5
WHEN 7 THEN WORKSTRT_6
WHEN 1 THEN WORKSTRT_7
END,
@ENDTIME = CASE @Day
WHEN 2 THEN WORKEND_1
WHEN 3 THEN WORKEND_2
WHEN 4 THEN WORKEND_3
WHEN 5 THEN WORKEND_4
WHEN 6 THEN WORKEND_5
WHEN 7 THEN WORKEND_6
WHEN 1 THEN WORKEND_7
END
FROM
SVC00100
WHERE
TECHID = @TechID

SELECT
@STARTTIME = CONVERT(VARCHAR(20), @InDate, 102) + ' ' + CONVERT(VARCHAR(20), @STARTTIME, 108),
@ENDTIME = CONVERT(VARCHAR(20), @InDate, 102) + ' ' + CONVERT(VARCHAR(20), @ENDTIME, 108)
END
ELSE
SELECT
@STARTTIME = @InDate,
@ENDTIME = @InDate

RETURN (0)
Go to Top of Page

Eyespi20
Starting Member

10 Posts

Posted - 2012-08-13 : 11:48:35
I have it figured out.... the time on the card was 00:00:00 for both start and end times, so the proc was returning 08/10/2012 00:00:00 for both start and end times which renders the person off unless the time on the call was exactly midnight.

Thank you everyone!



Margaret
    è¿é
Go to Top of Page
   

- Advertisement -