2013-07-26 29 views
0

我試圖通過一個給定參數From和To Date循環訪問cte。我讀了create while loop with cte並在我的查詢中嘗試過,但結果只顯示開始日期和下一個日期。帶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) 
    ) 
+0

只是調整你的陳述,它會創建多餘的日期。 「where dates.date <@todate」。謝謝,你給我一個解決方案。 –