Home > Database > Mysql Tutorial > How can we use prepared statements in stored procedures?

How can we use prepared statements in stored procedures?

WBOY
Release: 2023-08-23 17:45:14
forward
1332 people have browsed it

How can we use prepared statements in stored procedures?

If we want to use prepared statements in stored procedures, then it must be written inside BEGIN and END blocks. To understand it, we created an example where we can get all the records in a table by giving the table name as parameter to the stored procedure.

Example

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

Now call this procedure by passing the name of the table as parameter and it will display all the records of that table.

mysql> DELIMITER;
mysql> CALL tbl_detail('Student’);
+------+--------+
| Id   | Name   |
+------+--------+
|    1 | Ram    |
|    2 | Shyam  |
|    3 | Gaurav |
+------+--------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.03 sec)
Copy after login

The above is the detailed content of How can we use prepared statements in stored procedures?. 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