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

后端 未结 2 714
攒了一身酷
攒了一身酷 2021-01-28 18:16

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

WITH DepartmentCTE AS
(   SELECT  ID, 
        Department         


        
相关标签:
2条回答
  • 2021-01-28 18:46

    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.

    0 讨论(0)
  • 2021-01-28 18:54

    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.

    0 讨论(0)
提交回复
热议问题