- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Dynamic SQL query inside a MySQL stored procedure?
For dynamic SQL query in a stored procedure, use the concept of PREPARE STATEMENT. Let us first create a table −
mysql> create table DemoTable2033 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (1.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2033(Name) values('Chris'); Query OK, 1 row affected (0.85 sec) mysql> insert into DemoTable2033(Name) values('Bob'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2033(Name) values('David'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable2033(Name) values('Mike'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2033;
This will produce the following output −
+----+-------+ | Id | Name | +----+-------+ | 1 | Chris | | 2 | Bob | | 3 | David | | 4 | Mike | +----+-------+ 4 rows in set (0.00 sec)
Following is the query to create a stored procedure and implement dynamic SQL −
mysql> delimiter // mysql> create procedure dynamic_query() -> begin -> set @query=concat("select *from DemoTable2033 where Id=3"); -> prepare st from @query; -> execute st; -> end -> // Query OK, 0 rows affected (0.13 sec) mysql> delimiter ;
Call the stored procedure −
mysql> call dynamic_query();
This will produce the following output −
+----+-------+ | Id | Name | +----+-------+ | 3 | David | +----+-------+ 1 row in set (0.04 sec) Query OK, 0 rows affected (0.05 sec)
- Related Articles
- Implement Conditional MySQL Query in a stored procedure?
- Implement DELETE query in MySQL stored procedure
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Dynamic SQL to get a parameter and use it in LIKE for a new table created inside a stored procedure
- Implement If else in stored procedure in MySQL?
- How can we handle a result set inside MySQL stored procedure?
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- How to correctly implement conditions in MySQL stored procedure?
- Create a table inside a MySQL stored procedure and insert a record on calling the procedure
- How can we perform START transactions inside MySQL stored procedure?
- How can we perform COMMIT transactions inside MySQL stored procedure?
- MySQL Sum Query with IF Condition using Stored Procedure
- How to correctly implement END IF statement in a MySQL Stored Procedure?
- Get database name from a query implemented in a MySQL Stored Procedure?
- Calling Stored Procedure inside foreach PHP Codeigniter

Advertisements