Mysql5.7自定义函数递归报错1424 Recursive stored functions and triggers are not allowed

时间:2025-05-01 00:05:25

示例:

DELIMITER $$
CREATE FUNCTION test(countnum INT)
RETURNS INT DETERMINISTIC
BEGIN
DECLARE tempnum INT DEFAULT 0;
IF countnum > 2 THEN
RETURN ROW_COUNT();
END IF;
SET countnum = countnum+1;
SELECT test(countnum) INTO tempnum;
END $$
DELIMITER ;

SELECT test(1);

当我调用自定义函数时会抛出 Recursive stored functions and triggers are not allowed(不允许递归存储函数和触发器。)

函数是不支持递归,但是可以用存储过程递归

示例:

DELIMITER $$
CREATE PROCEDURE test(countnum INT)
end_flag:
BEGIN
DECLARE tempnum INT DEFAULT 0;
IF countnum > 2 THEN
SELECT '满足条件结束存储过程';
LEAVE end_flag;
END IF;
SET countnum = countnum+1;
CALL test(countnum);
END $$
DELIMITER ;

CALL test(1);

执行存储过程可能会抛出:

1456
Recursive limit 0 (as set by the max_sp_recursion_depth variable) was exceeded for routine test

max_sp_recursion_depth :递归调用的最大深度

可以执行:SET GLOBAL max_sp_recursion_depth =层级数;