SSMS存储过程不显示结束日期的记录

时间:2016-08-30 04:59:40

标签: sql sql-server-2008 ssms

我有一个存储过程,它接受两个参数:开始日期(@SDate)和结束日期(@EDate),之后它会输出两个日期之间的数据摘要。

但是,它不会显示@EDate上的记录。

这是我的代码:

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;

1 个答案:

答案 0 :(得分:1)

原因是你传递的参数可能是没有时间指示的日期,因此它们是午夜引用。任何具有时间参考的日期(如日志时间的情况)将被视为以后而不是午夜参考。

例如,以下情况属实:

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

解决方案很简单。而不是

between @SDate and @EDate

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