
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 //
Query OK, 0 rows affected (0.00 sec)
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 //
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> Call get_factorial(10);
+--------------+
| @factorial |
+--------------+
| 3628800 |
+--------------+
1 row in set (0.11 sec)
Query OK, 0 rows affected (0.12 sec)
mysql> Call get_factorial(5);
+-------------+
| @factorial |
+-------------+
| 120 |
+-------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)以上就是我们如何创建MySQL存储过程来计算阶乘?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号