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(
IN number INT,
OUT total INT
) BEGIN
DECLARE score INT DEFAULT NULL;
SELECT awards FROM achievements
WHERE id = number INTO score;
IF score IS NULL THEN SET total = 0;
ELSE
CALL calctotal(number+1);
SET total = total + score;
END IF;
END $$
DELIMITER ;