Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL does not support recursive functions? why? since when?

I've written a stored FUNCTION that calls itself, recursively.

However when I run it in a query I get this shameless error:

Error: 1424 SQLSTATE: HY000 (ER_SP_NO_RECURSION)

Message: Recursive stored functions and triggers are not allowed.

"Not allowed"?
Right. Why don't we just disable WHILE loops also, while we're at it?

Can I enable recursive functions in any way?
I found a bug report, but are there any workarounds?
I'm running MySQL 5.1.41 on Windows XP (XAMPP Server).

like image 582
Robin Rodricks Avatar asked Aug 21 '10 05:08

Robin Rodricks


People also ask

Does MySQL support recursive queries?

MySQL does offer recursive common table expressions, but compared to SQL Server CTE's, they have major limitations and won't solve this problem. MySQL functions do not handle recursion at all. This article will explore all of these options.

Does MySQL 5.7 support with recursive?

MySQL version 5.7 does not offer such a feature.

How do you write a recursive function in MySQL?

MySQL does not allow recursive FUNCTIONs, even if you set max_sp_recursion_depth. It does allow up to 255 recursion in a PROCEDURE if you set max_sp_recursion_depth. So I recommend that you replace your function with a procedure, using an INOUT variable for the return_path. Show activity on this post.

Why are recursive functions inefficient?

Recursive algorithms are often inefficient for small data, due to the overhead of repeated function calls and returns. For this reason efficient implementations of recursive algorithms often start with the recursive algorithm, but then switch to a different algorithm when the input becomes small.


1 Answers

MySQL 5.1 supports recursive stored procedures, but not recursive functions. Quoting the docs:

Stored functions cannot be recursive.

Recursion in stored procedures is permitted but disabled by default. To enable recursion, set the max_sp_recursion_depth server system variable to a value greater than zero. Stored procedure recursion increases the demand on thread stack space. If you increase the value of max_sp_recursion_depth, it may be necessary to increase thread stack size by increasing the value of thread_stack at server startup.

like image 193
Daniel Vassallo Avatar answered Nov 15 '22 08:11

Daniel Vassallo