CTE使用SQL Server获取两个日期之间的日期

use*_*125 9 sql-server-2008

我想在两个日期之间获取缺少的日期

@maxDate = '2013-01-28'

@curDate = GetDate()

我写了CTE来做这件事.这是我的CTE:

create procedure EmpDate  
as  begin  
declare @curDate Date  
set @curDate = GETDATE()  
declare @maxDate Date  
select @maxDate = MAX(EmpAttendance.Sdate)  
from EmpAttendance

;with GetDates As  
(  
select 1 as counter, @maxDate as Date   
UNION ALL  
select counter + 1, DATEADD(day,counter,@maxDate)  
from GetDates  
where DATEADD(day, counter, @maxDate) < @curDate  
)  
select Date from GetDates  
end   
go  
Run Code Online (Sandbox Code Playgroud)

结果是

Date  
2013-01-28  
2013-01-29  
2013-01-30  
Run Code Online (Sandbox Code Playgroud)

但我想要

2013-01-29  
2013-01-30   
Run Code Online (Sandbox Code Playgroud)

请帮帮我.

gbn*_*gbn 9

更改

select 1 as counter, @maxDate as Date
Run Code Online (Sandbox Code Playgroud)

select 1 as counter, DATEADD(day,1,@maxDate) as Date
Run Code Online (Sandbox Code Playgroud)

尽管改变CTE使其更简单

;with GetDates As  
(  
select DATEADD(day,1,@maxDate) as TheDate
UNION ALL  
select DATEADD(day,1, TheDate) from GetDates  
where TheDate < @curDate  
)
... 
Run Code Online (Sandbox Code Playgroud)