How can I get all the records of a table by passing its name as the parameter of MySQL stored procedure?


Suppose if we want to see all the records of a table by passing its name as the parameter of a stored procedure then following example will create a procedure named ‘details’ which accepts the name of the table as its parameter −

mysql> DELIMITER //
mysql> Create procedure details(tab_name Varchar(40))
   -> BEGIN
   -> SET @t:= CONCAT('Select * from',' ',tab_name);
   -> Prepare stmt FROM @t;
   -> EXECUTE stmt;
   -> END //
Query OK, 0 rows affected (0.00 sec)

Now invoke this procedure by giving the name of the table as its parameter and it will show all the records of that table.

mysql> DELIMITER;
mysql> CALL details('student_detail');
+-----------+-------------+------------+
| Studentid | StudentName | address    |
+-----------+-------------+------------+
|       100 | Gaurav      | Delhi      |
|       101 | Raman       | Shimla     |
|       103 | Rahul       | Jaipur     |
|       104 | Ram         | Chandigarh |
|       105 | Mohan       | Chandigarh |
+-----------+-------------+------------+
5 rows in set (0.02 sec)
Query OK, 0 rows affected (0.03 sec)

Updated on: 22-Jun-2020

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements