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
avatar
Sort by:   newest | oldest | most voted
lasertest
Guest

Hey very nice blog!

lasertest
Guest

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!

ronak
Guest

Thanks Anvesh, You helped me !

lasertest
Guest

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.

KIMAK KAU
Guest

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.

nofireretardent
Guest

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…

Melia
Guest

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.

lasertest
Guest

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!

lasertest
Guest

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
Guest

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
Guest

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

sdorttuii plmnr
Guest

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.

sdorttuiiplmnr
Guest

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!

shana
Guest

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.

octavia
Guest

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.

weed
Guest

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.

merri
Guest

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

lorrileslie
Guest

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?

pode
Guest

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.

brush
Guest

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

kerri
Guest

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

clarie
Guest

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

kendal
Guest

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

Tod
Guest

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

john
Guest

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.

Teena
Guest

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

Maria
Guest

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!

kaley
Guest

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.

trackback

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

wpDiscuz