技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

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

mysqlmysqli database

要抑制 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)

相关文章