- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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)
- 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?
- What is stored procedure and how can we create MySQL stored procedures?
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- How can we handle a result set inside MySQL stored procedure?
- Use delimiter correctly in a MySQL stored procedure to avoid BEGIN/END statements errors
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How can we perform START transactions inside MySQL stored procedure?
- How can we perform COMMIT transactions inside MySQL stored procedure?
- Can we use a comma between MySQL SELECT statements?
- How can we create MySQL stored procedure to calculate the factorial?
- How can we see the source code of a particular MySQL stored procedure?