我有一個存儲過程查詢我的數據庫以便爲「Yesterdays」日期提取「打開」報告。這個程序將運行在Windows調度器上,總是查詢數據庫中的昨天結果。除了昨天上午5:30 - 上午6:26之外,我想要得到昨天的所有結果。我不小心看到這些結果...一切正常,用不顯示在指定的時間塊除外...在存儲過程中排除特定時間範圍
我的存儲過程是:
ALTER PROCEDURE [dbo].[Open_Close] AS
declare @dtNow datetime , @dtToday datetime , @dtFrom datetime , @dtThru datetime , @dtExcludeFrom datetime , @dtExcludeThru datetime
set @dtNow = getdate()
set @dtToday = convert(datetime,convert(varchar,@dtNow,112),112)
set @dtFrom = dateadd(day,-1,@dtToday) -- start-of-day yesterday
set @dtThru = dateadd(ms,-3,@dtToday) -- end-of-day yesterday (e.g., 2012-06-17 23:59:59.997)
set @dtExcludeFrom = convert(datetime, convert(char(10),@dtFrom,120) + ' 05:30:00.000' , 120)
set @dtExcludeThru = convert(datetime, convert(char(10),@dtFrom,120) + ' 06:15:00.000' , 120)
SELECT Store_Id , DM_Corp_Received_Date
FROM Register_Till_Count_Tb
WHERE Register_Transaction_Type = 'SOD'
AND Register_Till_Count_Datetime between @dtFrom
and @dtThru AND Register_Till_Count_Datetime not between @dtExcludeFrom
and @dtExcludeThru
我不認爲這就是問題所在。日期正在計算正確,並且WHERE子句設置爲按照你的意圖進行。 – 2012-08-09 15:49:22