How to create recursion in Mysql Procedures?
SET @@ GLOBAL .max_sp_recursion_depth = 255;
SET @@session.max_sp_recursion_depth = 255;
thread_stack = 64M
or
thread_stack = 128M
|
Example Factorial Program with Recursive Algorithm
DROP PROCEDURE IF EXISTS find_fact;
DROP PROCEDURE IF EXISTS factorial;
DELIMITER $$
CREATE PROCEDURE find_fact(IN n INT)
BEGIN
SET @@GLOBAL.max_sp_recursion_depth = 255;
SET @@session.max_sp_recursion_depth = 255;
CALL factorial(n,@fact);
SELECT @fact;
END
$$
DELIMITER $$
CREATE PROCEDURE factorial(IN n INT,OUT fact INT)
BEGIN
IF n = 1 THEN
SET fact := 1;
ELSE
CALL factorial(n-1,fact);
SET fact := n * fact;
END IF;
END
$$
|
Calling Procedure & Output
CALL find_fact(5);
|