MySQL的阶乘函数,我们如何创建MySQL存储过程来计算阶
乘?
mysql> DELIMITER //
mysql> CREATE PROCEDURE get_factorial(IN N INT)
-> BEGIN
->    SET @@GLOBAL.max_sp_recursion_depth = 255;
->    SET @@session.max_sp_recursion_depth = 255;
->
->    CALL factorial_recursive (N, @factorial);
->
->    SELECT @factorial;
-
> END //
mysql> DELIMITER //
mysql> CREATE PROCEDURE factorial_recursive(IN N INT,OUT factorial INT)
-> BEGIN
->    IF N = 1 THEN
->      SET factorial := 1;
->    ELSE
->      CALL factorial_recursive (N-1, factorial);
->      SET factorial := N * factorial;
->    END IF;
-> END //
mysql> DELIMITER ;mysql存储过程使用
mysql> Call get_factorial(10);
+--------------+
| @factorial  |
+--------------+
|      3628800 |
+--------------+
1 row in set (0.11 sec)
mysql> Call get_factorial(5);
+-------------+
| @factorial  |
+-------------+
|        120 |
+-------------+
1 row in set (0.00 sec)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。