如何抑制MySQL存储过程输出?

如何抑制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,'约翰');
Query OK, 1 row affected (0.17 sec)

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

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

以下是使用select命令从表中显示记录的查询:

mysql> select *from person_information;

这会产生以下输出

+------+--------+
| Id | Name     |
+------+--------+
| 100 | 约翰    |
| 101 | 克里斯   |
| 102 | 罗伯特  |
+------+--------+
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 |
+---------+
| 克里斯   |
+---------+
1 row in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程