Wednesday, 25 July 2018

How to write Dynamic SQL Query in MySQL Stored Procedure

I received many emails on, how can I write dynamic SQL in MySQL Stored Procedure.
I understood that the database professional tried to find Dynamic SQL in MySQL like SQL Server or PostgreSQL.
MySQL Doesn’t Support dynamic SQL in Stored Procedure. Even dynamic SQL is not good for database security because of SQL Injections.
You have to write prepared statement for this requirement.
In this post, I am sharing demonstration on a dynamic SQL as a prepared statement in the MySQL Stored Procedure.
First, create a sample table and data
Now, create a stored procedure and pass the column name dynamically:
Call this stored procedure by giving desire column name and it will return only data for that column:


MySQL Dynamic SQL

0 comments:

Post a Comment