Home > Database > Mysql Tutorial > body text

How can we access the table through MySQL stored procedure?

WBOY
Release: 2023-08-29 16:45:02
forward
1293 people have browsed it

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template