MySQL Stored procedure won't fetch the whole table?


You can use SELECT *FROM yourTableName in stored procedure. Let us first create a table −

mysql> create table DemoTable654 (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,StudentFirstName varchar(100)
);
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable654(StudentFirstName) values('John');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable654(StudentFirstName) values('Sam');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable654(StudentFirstName) values('Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable654(StudentFirstName) values('Robert');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable654;

This will produce the following output −

+-----------+------------------+
| StudentId | StudentFirstName |
+-----------+------------------+
|         1 | John             |
|         2 | Sam              |
|         3 | Mike             |
|         4 | Robert           |
+-----------+------------------+
4 rows in set (0.00 sec)

Following is the query to fetch the records of entire table using stored procedure −

mysql> DELIMITER //
mysql> CREATE PROCEDURE getWholeTable()
   BEGIN
   SELECT *FROM DemoTable654;
   END
   //
Query OK, 0 rows affected (0.16 sec)
mysql> DELIMITER ;

Now you can call the above stored procedure using call command −

mysql> call getWholeTable();

This will produce the following output −

+-----------+------------------+
| StudentId | StudentFirstName |
+-----------+------------------+
| 1         | John             |
| 2         | Sam              |
| 3         | Mike             |
| 4         | Robert           |
+-----------+------------------+
4 rows in set (0.08 sec)
Query OK, 0 rows affected (0.13 sec)

Updated on: 23-Aug-2019

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements