如何抑制 MySQL 儲存過程輸出?


要抑制 MySQL 儲存過程的輸出,你可以使用變數。我們首先建立一個表。

mysql> create table person_information
   -> (
   -> Id int,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.50 sec)

以下是使用 insert 命令在表中插入一些記錄的查詢

mysql> insert into person_information values(100,'John');
Query OK, 1 row affected (0.17 sec)

mysql> insert into person_information values(101,'Chris');
Query OK, 1 row affected (0.22 sec)

mysql> insert into person_information values(102,'Robert');
Query OK, 1 row affected (0.16 sec)

以下是使用 select 命令從表中顯示記錄的查詢

mysql> select *from person_information;

這將產生以下輸出

+------+--------+
| Id | Name     |
+------+--------+
| 100 | John    |
| 101 | Chris   |
| 102 | Robert  |
+------+--------+
3 rows in set (0.00 sec)

以下是抑制 MySQL 儲存過程輸出的查詢

mysql> DELIMITER //
mysql> CREATE PROCEDURE sp_supressOutputDemo()
   -> BEGIN
   -> set @output=(select Name from person_information where id=101);
   -> END
   -> //
Query OK, 0 rows affected (0.14 sec)

mysql> DELIMITER ;

你可以使用 CALL 命令呼叫上述儲存過程

mysql> call sp_supressOutputDemo();
Query OK, 0 rows affected (0.00 sec)

在呼叫上述儲存過程後,我們什麼也沒有得到。因此,你需要使用 select 語句來獲取輸出。

以下是查詢

mysql> select @output;

這將產生以下輸出

+---------+
| @output |
+---------+
| Chris   |
+---------+
1 row in set (0.00 sec)

更新於:30-Jul-2019

924 次瀏覽

啟動你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.