首頁 >資料庫 >mysql教程 >我們如何建立MySQL預存程序來計算階乘?

我們如何建立MySQL預存程序來計算階乘?

WBOY
WBOY轉載
2023-08-23 08:13:021609瀏覽

我們如何建立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 //
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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除