How to write Dynamic SQL Query in MySQL Stored Procedure

I receive many emails on, how can I write dynamic SQL in MySQL Stored Procedure.
I understand that the database professional tries 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.

Please visit this article for Prepared Statements.

Prepared or Parameterized Statements in Database System

In this post, I demonstrate dynamic SQL as a prepared statement in MySQL Stored Procedure.

Let’s show me a small example of this:

First, create sample table and data.

Now create one stored procedure to pass the column name dynamically.

Call this stored procedure by giving desire column name and it will return only that column data.

MySQL Dynamic SQL

Anvesh Patel

Leave a Reply

33 Comments on "How to write Dynamic SQL Query in MySQL Stored Procedure"

Notify of
Sort by:   newest | oldest | most voted

Hey very nice blog!


Right here is the right blog for anybody who really wants to understand this topic.
You realize a whole lot its almost hard to argue with you (not that I actually would want to…HaHa).
You definitely put a fresh spin on a topic that’s been discussed for years.
Great stuff, just great!


Thanks Anvesh, You helped me !


It is in point of fact a nice and useful piece of
information. I am happy that you shared this helpful information with us.

Please stay us informed like this. Thanks for sharing.


Hi, i read your blog occasionally and i own a similar one and i was just wondering
if you get a lot of spam comments? If so how do you reduce it, any plugin or anything you can suggest?
I get so much lately it’s driving me crazy so any support is very
much appreciated.


I wanted to thank you for this fantastic read!!
I definitely enjoyed every bit of it. I have you saved as
a favorite to check out new things you post…


I just want to tell you that I am newbie to blogging and site-building and definitely savored your website. Probably I’m going to bookmark your website . You actually come with wonderful writings. Thanks a bunch for sharing with us your blog site.


Hi there! I know this is kinda off topic however I’d
figured I’d ask. Would you be interested in trading
links or maybe guest writing a blog post or vice-versa?
My website addresses a lot of the same subjects
as yours and I think we could greatly benefit from each other.
If you might be interested feel free to send me an e-mail.
I look forward to hearing from you! Fantastic blog by the way!


I am curious to find out what blog system you are working with?
I’m experiencing some minor security issues with my latest blog and I’d like to
find something more safeguarded. Do you have any recommendations?

buy a college degree

You actually make it seem so easy with your presentation but I find this topic to be actually
something that I think I would never understand. It seems too complicated and extremely
broad for me. I’m looking forward for your next post, I’ll try to get
the hang of it!

facebook marketing

Very good info. Lucky me I recently found your website by chance (stumbleupon).
I’ve saved it for later!

sdorttuii plmnr

I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.


You actually make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!


Heya i’m for the primary time here. I came across this
board and I in finding It really helpful & it helped me out a
lot. I hope to provide one thing back and
help others such as you helped me.


Just want to say your article is as astounding. The clearness in your post is jusst great
and i caan assume you are an expert oon tis subject.
Well with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please carry onn the rewarding work.


An outstanding share! I have just forwarded this onto a co-worker who had been conducting a little homework on this.
And he in fact ordered me lunch due to the fact that
I found it for him… lol. So let me reword
this…. Thank YOU for the meal!! But yeah, thanks for spending
the time to discuss this topic here on your website.


This article will help the internet visitors for building up new weblog
or even a blog from start to end.


excellent points altogether, you simply received a brand new
reader. What may you suggest in regards to your publish that
you simply made a few days in the past? Any sure?


Your style is so unique in comparison to other people I’ve read stuff from.
Thanks for posting when you’ve got the opportunity, Guess I’ll
just bookmark this blog.


I used to be able to fnd good information from your blog


What a material of un-ambiguity and preserveness of valuable know-how concerning
unexpected feelings.


Thanks designed for sharing such a pleasant thought, paragraph is good,
thats why i have read it completely


I savor, cause I found exactly what I was looking for. You have ended my 4 day long hunt!
God Bless you man. Have a nice day. Bye


Yeah bookmaking this wasn’t a bad determination outstanding post! .


Simply want to say your article is as surprising. The clarity in your post is simply
nice and i could assume you are an expert on this subject.
Fine with your permission let me to grab your feed to keep up to date with forthcoming post.
Thanks a million and please keep up the gratifying work.


Excellent, what a website it is! This blog presents useful data to us, keep it
up. Teena


This is a good tip particularly to those fresh to the blogosphere.

Simple but very accurate information… Many thanks for sharing this one.
A must read article!


Hi it’s me, I am also visiting this site on a regular basis, this website is really fastidious
and the visitors are genuinely sharing pleasant thoughts.


thanks for the information.
please provide more detail on parameterized query