SQL Server : Recursive CTE and Maximum Recursion

Default maximum recursion is 100. This can be altered by setting the maxrecursion as an option. If the recursion has gone past the maximum then you will receive the following error:

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

Here is an example with maximum recursion set to 1000.

;with cte(N) as
select 1 as N
union all
select cte.N + 1
from cte
where cte.N <1000
select * from cte
option (maxrecursion 1000)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s