Friday, May 28, 2021

The maximum recursion has been exhausted before statement completion

OPTION (MAXRECURSION n);

While executing above statement with CTE (common table expression) or recursive CTE, error came like below

Msg 530, Level 16, State 1, Line 1

The statement terminated. The maximum recursion %d has been exhausted before statement completion.

Let's see the below example where try to control the recursion (think stack overflow in code) with MAXRECURSION as a query option that will limit the number of recursive calls.

WITH yearsAgo
(
myYear
)
AS
(
-- Base Case
SELECT DATEPART(year , GETDATE()) AS myYear
UNION ALL
-- Recursive Section
SELECT yearsAgo.myYear - 1
FROM yearsAgo
WHERE yearsAgo.myYear >= 2002
)
SELECT * FROM yearsAgo
OPTION (MAXRECURSION 10);

Output:

Msg 530, Level 16, State 1, Line 1

The statement terminated. The maximum recursion 10 has been exhausted before statement completion.


No comments:

Post a Comment