The maximum recursion 100 has been exhausted before statement completion error showing in SQL Query

白昼怎懂夜的黑 提交于 2019-12-02 19:19:20

问题


"The maximum recursion 100 has been exhausted before statement completion" error showing in SQL Query

WITH DepartmentCTE AS
(   SELECT  ID, 
        DepartmentName, 
        RootID, 
        RecursionLevel = 1, 
        ParentRoot = CAST('None' AS NVARCHAR(max)),
        LastParentCatID = RootID,
        DisplayOrder
FROM    Department
UNION ALL
SELECT  cte.ID, 
        cte.DepartmentName,
        cte.RootID,
        cte.RecursionLevel + 1,
        ParentRoot = CASE WHEN cte.RecursionLevel = 1 THEN '' ELSE cte.ParentRoot + '>' END + c.DepartmentName,
        LastParentCatID = c.RootID,
        cte.DisplayOrder
FROM    DepartmentCTE cte
        INNER JOIN Department c
            ON c.ID = cte.RootID

), MaxRecursion AS
(   SELECT  ID, 
        DepartmentName, 
        RootID, 
        ParentRoot, 
        RowNum = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY RecursionLevel DESC),
        DisplayOrder
FROM    DepartmentCTE
)
SELECT  ID, DepartmentName, RootID, ParentRoot
FROM    MaxRecursion 
WHERE   RowNum = 1;

回答1:


You can limit the number of recursion levels using the MAXRECURSION option hint like this: OPTION (MAXRECURSION 0); where the value (between 0 and 32767) specifies the number of levels of recursion, 0 meaning infinite.

From the documentation for CTE:

An incorrectly composed recursive CTE may cause an infinite loop. For example, if the recursive member query definition returns the same values for both the parent and child columns, an infinite loop is created. To prevent an infinite loop, you can limit the number of recursion levels allowed for a particular statement by using the MAXRECURSION hint and a value between 0 and 32,767 in the OPTION clause of the INSERT, UPDATE, DELETE, or SELECT statement. This lets you control the execution of the statement until you resolve the code problem that is creating the loop. The server-wide default is 100. When 0 is specified, no limit is applied. Only one MAXRECURSION value can be specified per statement. For more information, see Query Hints (Transact-SQL).

And the documentation for the query hints states:

MAXRECURSION number

Specifies the maximum number of recursions allowed for this query. Number is a nonnegative integer between 0 and 32767. When 0 is specified, no limit is applied. If this option is not specified, the default limit for the server is 100.

When the specified or default number for MAXRECURSION limit is reached during query execution, the query is ended and an error is returned.

Because of this error, all effects of the statement are rolled back. If the statement is a SELECT statement, partial results or no results may be returned. Any partial results returned may not include all rows on recursion levels beyond the specified maximum recursion level.

To use the statement you append the OPTION clause after the FROM clause in the query using the recursive CTE.

Specifying 0 might lead to bad stuff if the query goes into an infinite loop though.




回答2:


Not sure if this is what you intended, but realize the DepartmentCTE CTE “calls” itself because the second part of its union is “From DepartmentCTE”. This is very useful behavior if intended and really bad if not intended. In your case I don’t see anything limiting the recursion. The CTE would juts call itself indefinitely . If you use the recursion, normally there would be some kind of limiting statement like an “If Level...” or “If Exists…). The recursion level of 100 is quite generous for DB environment and would agree with @jpw that turning it off would be bad. IN your case really would be an infinite loop until process crashed or something like it.

Was the looping your intent? If not, remove the DepartmentCTE in some way. If so then find how to limit in based one when you are “done”. If not sure maybe give some more info about goal to see if we can figure out.



来源:https://stackoverflow.com/questions/19220778/the-maximum-recursion-100-has-been-exhausted-before-statement-completion-error-s

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