How to suppress MySQL stored procedure output?

MySQLMySQLi Database

To suppress MySQL stored procedure output, you can use variable. Let us first create a table.

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

Following is the query to insert some records in the table using insert command:

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)

Following is the query to display records from the table using select command:

mysql> select *from person_information;

This will produce the following output

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

Here is the query to suppress MySQL stored procedure output:

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 ;

You can call the above stored procedure using CALL command:

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

After calling the above stored procedure, we are not getting anything. Therefore, you need to use select statement to get the output.

Following is the query

mysql> select @output;

This will produce the following output

+---------+
| @output |
+---------+
| Chris   |
+---------+
1 row in set (0.00 sec)
raja
Published on 17-Apr-2019 14:28:38
Advertisements