0

我得到這個錯誤:TSQL MAXRECURSION在CTE

The maximum recursion 100 has been exhausted before statement completion

當我運行這個功能:

WITH allDays AS (

     SELECT @DateEarly AS date 

     UNION ALL 

     SELECT DATEADD(dd, 1, date) as date 
     FROM allDays s 
     WHERE DATEADD(dd, 1, date) <= @DateLate 


    ) 
    SELECT * 
    from allDays 
    where dbo.isFestivo(date)>0 

我試圖追加此選項:

OPTION (MAXRECURSION 200); 

,但我得到「選項」字之前的錯誤156。 你知道爲什麼嗎?

+2

**你究竟在哪裏*追加*此選項(MAXRECURSION 200)?你能告訴我們你有*這個選項的查詢嗎? –

回答

6

你可能會把選項放在錯誤的地方。它需要在哪裏之後

WITH allDays AS (
    SELECT @DateEarly AS date 
    UNION ALL 
    SELECT DATEADD(dd, 1, date) as date 
    FROM allDays s 
    WHERE DATEADD(dd, 1, date) <= @DateLate 
) 
SELECT * 
from allDays 
where dbo.isFestivo(date)>0 
option (maxrecursion 200); 

但是試試這個。它會更快...

select DATEADD(d, number, @dateearly) as [date] 
from master..spt_values 
where type='p' 
and number<=datediff(d,@dateearly,@datelate) 
and dbo.isFestivo(date)>0 
+0

第二個工程(但我不得不改變dbo.isFestivo(日期)與dbo.isFestivo(DATEADD(d,編號,@dateearly)))! – Tobia