0 votes
in Sql by
What is a Recursive Stored Procedure?

1 Answer

0 votes
by

A stored procedure which calls itself until a boundary condition is reached, is called a recursive stored procedure. This recursive function helps the programmers to deploy the same set of code several times as and when required. Some SQL programming languages limit the recursion depth to prevent an infinite loop of procedure calls from causing a stack overflow, which slows down the system and may lead to system crashes.

DELIMITER $$ /* Set a new delimiter => $$ */

CREATE PROCEDURE calctotal( /* Create the procedure */

    IN number INT, /* Set Input and Ouput variables */

    OUT total INT

) BEGIN

DECLARE score INT DEFAULT NULL; /* Set the default value => "score" */

SELECT awards FROM achievements /* Update "score" via SELECT query */

WHERE id = number INTO score;

IF score IS NULL THEN SET total = 0; /* Termination condition */

ELSE

CALL calctotal(number+1); /* Recursive call */

SET total = total + score; /* Action after recursion */

END IF;

END $$ /* End of procedure */

DELIMITER ; /* Reset the delimiter */

Related questions

0 votes
0 votes
asked Jun 15, 2023 in Sql by Robin
0 votes
asked Sep 19, 2023 in Dot Net by Robin
...