Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Is there a limit to recursion in sql?


Asked by Miguel Whitney on Dec 10, 2021 SQL



There is a limit for recursion. It defaults to 100, but could be extended with MAXRECURSION option (MS SQL Server specific). Practically, it could be a bad idea to crank recursion limit up. Graphs might have cycles and limited recursion depth can be a good defense mechanism to stop poorly behaving query.
Just so,
There is no recursion limit when using cte's. there is a default limit that can be extended by using the MAXRECURSION hint to 32,767. However if you set the MAXRECURSION to zero there is no recursive limit. Be careful though as there is a default limit for a reason.
In fact, This is recursion in action. Different DBMS products implement recursive SQL in different ways. Recursion is implemented in standard SQL-99 using common table expressions (CTEs). DB2, Microsoft SQL Server, Oracle and PostgreSQL all support recursive queries using CTEs.
In addition,
However, because of the difficulty developers can have understanding recursion, it is sometimes thought of as “too inefficient to use frequently.” But, if you have a business need to walk or explode hierarchies in your database, recursive SQL will likely be your most efficient option.
Subsequently,
You reach the maximum recursion depth you specified using the MAXRECURSION query hint (a value between 1 and 32767) and an error occurs. You set the maximum recursion depth to have no limit using the MAXRECURSION query hint (a value of 0) and crash your server due to an infinite loop.