问题
I get this error:
The maximum recursion 100 has been exhausted before statement completion
when I run this function:
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
I tried to append this option:
OPTION (MAXRECURSION 200);
but i get an error 156 before "option" word. Do you know why?
回答1:
You're probably putting the option in the wrong place. It needs to be after the where
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);
But try this instead. It'll be quicker...
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
来源:https://stackoverflow.com/questions/13396388/tsql-maxrecursion-on-a-cte