我试图使用给定的参数From和To Date循环一个cte。我读了这个create while loop with cte并在我的查询中尝试了它,但结果只显示了开始日期和下一个日期。
DECLARE @fromdate datetime
DECLARE @todate datetime
SET @fromdate='2013-07-23'
SET @todate='2013-07-24'
;with dates(num) as (
select @fromdate as fromdate
union all
select @fromdate + 1
from dates
where dates.num < @todate
),
T(employee_id,actualogin,actuallogout) as (
select d.*
from dates cross apply
dbo.ufn_GET_ATTENDANCE(dates.num) d
)
SELECT * FROM T
OPTION (MAXRECURSION 0)
答案 0 :(得分:1)
我倾向于首先获取日期列表然后应用函数:
with dates as (
select @fromdate as date
union all
select date + 1
from dates
where dates.date < @todate
),
T(employee_id,actualogin,actuallogout) as (
select t.*
from dates cross apply
dbo.ufn_GET_ATTENDANCE(dates.date)
)