MySQL procedure with SELECT to return the entire table

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1971
   (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(20),
   StudentPassword int
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1971(StudentName,StudentPassword) values('John','123456');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1971(StudentName,StudentPassword) values('Chris','123456');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1971(StudentName,StudentPassword) values('David','123456');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1971(StudentName,StudentPassword) values('Mike','123456');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1971;

This will produce the following output −

+-----------+-------------+-----------------+
| StudentId | StudentName | StudentPassword |
+-----------+-------------+-----------------+
|         1 | John        |          123456 |
|         2 | Chris       |          123456 |
|         3 | David       |          123456 |
|         4 | Mike        |          123456 |
+-----------+-------------+-----------------+
4 rows in set (0.00 sec)

Here is the query to create a stored procedure −

mysql> delimiter //
mysql> create procedure returnAll(pass varchar(30))
   begin
   select * from DemoTable1971 where StudentPassword=pass;
   end
   //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;

Now you can call a stored procedure using CALL command −

mysql> call returnAll('123456');

This will produce the following output displaying the entire table −

+-----------+-------------+-----------------+
| StudentId | StudentName | StudentPassword |
+-----------+-------------+-----------------+
|         1 | John        |          123456 |
|         2 | Chris       |          123456 |
|         3 | David       |          123456 |
|         4 | Mike        |          123456 |
+-----------+-------------+-----------------+
4 rows in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
raja
Published on 31-Dec-2019 12:18:45
Advertisements