TSQL Maxrecursion on a cte

瘦欲@ 提交于 2019-12-25 12:01:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!