Display table records from a stored procedure in MySQL


Let us first create a table −

mysql> create table DemoTable1933
   (
   ClientName varchar(20)
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1933 values('Chris Brown');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1933 values('David Miller');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1933 values('Adam Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1933 values('John Doe');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1933;

This will produce the following output −

+--------------+
| ClientName   |
+--------------+
| Chris Brown  |
| David Miller |
| Adam Smith   |
| John Doe     |
+--------------+
4 rows in set (0.00 sec)

Here is the query to create a stored procedure and set SELECT in it to display records −

mysql> delimiter //
mysql> create procedure display_all_records()
   begin
   select * from DemoTable1933;
   end
   //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

Now you can call a stored procedure using call command:

mysql> call display_all_records();

This will produce the following output −

+--------------+
| ClientName   |
+--------------+
| Chris Brown  |
| David Miller |
| Adam Smith   |
| John Doe     |
+--------------+
4 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

Updated on: 30-Dec-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements