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.
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)
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)
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!