Home  >  Article  >  Database  >  How can we access the table through MySQL stored procedure?

How can we access the table through MySQL stored procedure?

WBOY
WBOYforward
2023-08-29 16:45:021264browse

How can we access the table through MySQL stored procedure?

We can access one or all tables from MySQL stored procedures. Below is an example where we have created a stored procedure which will accept the name of the table as parameter and after calling it, will generate a result set containing all the details of the table.

Example

mysql> Delimiter //
mysql> Create procedure access(tablename varchar(30))
   -> BEGIN
   -> SET @X := CONCAT('Select * from',' ',tablename);
   -> Prepare statement from @X;
   -> Execute statement;
   -> END//
Query OK, 0 rows affected (0.16 sec)

Now call the procedure using the name of the table we need to access as its parameter.

mysql> Delimiter ;

mysql> Call access('student_info');
+------+---------+----------+------------+
| id   | Name    | Address  | Subject    |
+------+---------+----------+------------+
| 101  | YashPal | Amritsar | History    |
| 105  | Gaurav  | Jaipur   | Literature |
| 125  | Raman   | Shimla   | Computers  |
+------+---------+----------+------------+
3 rows in set (0.02 sec)
Query OK, 0 rows affected (0.04 sec)

The above is the detailed content of How can we access the table through MySQL stored procedure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete