2016-08-30 81 views
0

我有一個存儲過程需要兩個參數:開始日期(@SDate)和結束日期(@EDate),之後它輸出兩個日期之間的數據摘要。SSMS存儲過程不顯示記錄在結束日期

但是,它不顯示落在@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

的原因是你傳遞的參數可能是日期沒有時間指示,所以他們是午夜引用。任何具有時間參考的日期(例如日誌時間的情況)將被認爲是之後的比午夜參考。

例如,以下爲真:

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

的解決方案是簡單的。取而代之的

between @SDate and @EDate 

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

啊啊啊,爲什麼我現在明白了。謝謝! – Junxian