1. 程式人生 > >Mysql5.7自定義函式遞迴報錯1424 Recursive stored functions and triggers are not allowed

Mysql5.7自定義函式遞迴報錯1424 Recursive stored functions and triggers are not allowed

示例: 

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 =層級數;