-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
DELIMITER //
CREATE PROCEDURE fac(INOUT n INTEGER)
BEGIN
    DECLARE tmp INTEGER;
    IF n > 1 THEN
        SET tmp = n - 1;
        CALL fac(tmp);
        SET n = n*tmp;
    ELSE
        SET n = 1;
    END IF;
END//
CREATE PROCEDURE fac2(IN n INTEGER)
BEGIN
    DECLARE tmp INTEGER;
    SET tmp = n;
    CALL fac(tmp);
    SELECT tmp;
END//
DELIMITER ;
SET MAX_SP_RECURSION_DEPTH=100;
CALL FAC2(7);
