简体   繁体   中英

SSMS Stored procedure not showing records that fall on end date

I have a stored procedure that takes in two parameters: start date ( @SDate ) and end date ( @EDate ), after which it outputs a summary of data between the two dates.

However, it does not show records that falls on the @EDate .

This is my code:

DECLARE TidCursor SCROLL CURSOR
FOR 
    SELECT DISTINCT  
        A.TID, P.ProjGroup, A.Location, P.MainSubCon
    FROM 
        [PRT].[dbo].[Personnel] P,[PRT].[dbo].[Attendance] A 
    WHERE 
        A.TID = P.TID 
        AND (A.Timein BETWEEN @SDate AND @EDate) 
        AND (A.Timeout BETWEEN @SDate AND @EDate) 
        AND (A.Timein IS NOT NULL AND A.timeout IS NOT NULL)
    ORDER BY 
        A.TID ASC   

OPEN TidCursor      

FETCH NEXT FROM TidCursor INTO @rec1_Tid, @rec1_PG, @rec1_Loc, @rec1_MainSubCon
.
.
.

DECLARE AttnCursor SCROLL CURSOR FOR 
    SELECT TimeIn, TimeOut, Location 
    FROM Attendance
    WHERE TID = @rec1_Tid 
      AND (Timein BETWEEN @SDate AND @EDate) 
      AND (Timeout BETWEEN @SDate AND @EDate) 
      AND (Timein IS NOT NULL AND timeout IS NOT NULL)  
    ORDER BY TimeIn

-- variables used to check for multiple logins in the same day
SET @curDate = '';
SET @preDate = '';

OPEN AttnCursor         

FETCH NEXT FROM AttnCursor INTO @indt, @outdt, @inloc   

WHILE (@@FETCH_STATUS = 0)
BEGIN
   .
   .
   .

   FETCH NEXT FROM AttnCursor INTO @indt, @outdt, @inloc
END    -- END WHILE (@@FETCH_STATUS = 0)

-- Closes the cursor
CLOSE AttnCursor; 
DEALLOCATE AttnCursor;

FETCH NEXT FROM TidCursor INTO @rec1_Tid, @rec1_PG, @rec1_Loc, @rec1_MainSubCon

END -- END WHILE (@@FETCH_STATUS = 0)

-- Closes the cursor
CLOSE TidCursor; 
DEALLOCATE TidCursor;

The reason is that the arguments you pass are probably dates without a time indication, and so they are midnight references. Any date that has a time reference, like is the case with log times, will be considered later than the midnight reference.

For example, the following is true:

2016-08-05 07:41:33 > 2016-08-05

The solution is simple. Instead of

between @SDate and @EDate

write

between @SDate and dateadd(d, 1, @EDate)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM