
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we use prepared statements in a stored procedure?
If we want to use prepared statements in a stored procedure then it must be written inside the BEGIN and END block. To understand it, we are creating an example with the help of which we can get all the records from a table by passing the name of the table as a parameter of 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)
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 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)
- Related Articles
- How can we use prepared statements in MySQL?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- How can we invoke MySQL stored procedure?
- How can we write MySQL handler in a stored procedure?
- Can we call stored procedure recursively?
- What is stored procedure and how can we create MySQL stored procedures?
- How can we handle a result set inside MySQL stored procedure?
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- How can we perform START transactions inside MySQL stored procedure?
- How can we perform COMMIT transactions inside MySQL stored procedure?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Can we use stored procedure to insert records into two tables at once in MySQL?
- Use delimiter correctly in a MySQL stored procedure to avoid BEGIN/END statements errors
- How can we create MySQL stored procedure to calculate the factorial?

Advertisements