Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 September How to write Dynamic SQL Query in MySQL Stored Procedure

How to write Dynamic SQL Query in MySQL Stored Procedure

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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.

Please visit below article on Prepared Statements:

Prepared or Parameterized Statements in Database System

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

1
2
3
4
5
6
7
8
CREATE TABLE tbl_TestDynamicSQL
(
EmpID INTEGER
,EmpName VARCHAR(255)
);
 
INSERT INTO tbl_TestDynamicSQL
VALUES (1,'ABC'),(2,'XYZ'),(3,'PQR'),(4,'RFQ');

Now, create a stored procedure and pass the column name dynamically:

1
2
3
4
5
6
7
8
9
10
11
12
DELIMITER $$
CREATE PROCEDURE usp_GetEmployeeDetailsDynamic
(
IN ColumnName VARCHAR(255)
)
BEGIN
SET @SQLText = CONCAT('SELECT ',ColumnName,' FROM tbl_TestDynamicSQL ');
PREPARE stmt FROM @SQLText;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;

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

1
CALL usp_GetEmployeeDetailsDynamic ('EmpName');

MySQL Dynamic SQL

Sep 15, 2015Anvesh Patel
Delete all duplicates rows except one in SQL ServerEnable and Disable Foreign key and Check constraint in SQL Server
Comments: 39
  1. lasertest
    September 22, 2015 at 10:35 am

    Hey very nice blog!

    • Anvesh Patel
      Anvesh Patel
      September 22, 2015 at 11:50 am

      Thank you, reader like you make me more confident !

  2. lasertest
    September 22, 2015 at 1:39 pm

    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!

    • Anvesh Patel
      Anvesh Patel
      September 22, 2015 at 4:28 pm

      Thank You !

  3. ronak
    September 26, 2015 at 6:37 pm

    Thanks Anvesh, You helped me !

  4. lasertest
    October 6, 2015 at 7:18 am

    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.

  5. KIMAK KAU
    October 23, 2015 at 9:45 am

    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.

  6. nofireretardent
    October 29, 2015 at 1:45 am

    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…

    • Anvesh Patel
      Anvesh Patel
      October 29, 2015 at 6:22 pm

      Your welcome !

  7. Melia
    November 1, 2015 at 9:36 am

    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.

  8. lasertest
    November 17, 2015 at 8:12 am

    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!

  9. lasertest
    November 20, 2015 at 5:43 am

    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?

    • Anvesh Patel
      Anvesh Patel
      November 20, 2015 at 6:49 pm

      I have setup this blog using wordpress.org and please use two factor login authentication for security.

  10. buy a college degree
    November 22, 2015 at 3:46 am

    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!

  11. facebook marketing
    November 25, 2015 at 2:28 am

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

  12. sdorttuii plmnr
    December 7, 2015 at 3:15 pm

    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.

  13. sdorttuiiplmnr
    December 10, 2015 at 8:32 pm

    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!

  14. shana
    December 13, 2015 at 10:58 pm

    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.

  15. octavia
    January 1, 2016 at 1:40 pm

    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.

  16. weed
    January 5, 2016 at 6:58 pm

    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.

  17. merri
    January 6, 2016 at 7:07 pm

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

  18. lorrileslie
    January 8, 2016 at 6:51 am

    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?

  19. pode
    January 8, 2016 at 3:39 pm

    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.

  20. brush
    January 11, 2016 at 9:08 am

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

  21. kerri
    January 20, 2016 at 6:28 am

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

  22. clarie
    January 30, 2016 at 2:20 am

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

  23. kendal
    January 31, 2016 at 6:13 am

    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

  24. Tod
    February 5, 2016 at 7:07 pm

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

  25. john
    February 11, 2016 at 8:06 am

    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.

  26. Teena
    February 17, 2016 at 3:38 am

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

  27. Maria
    March 22, 2016 at 10:07 am

    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!

  28. kaley
    March 30, 2016 at 5:48 pm

    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.

  29. Srini
    February 20, 2018 at 4:10 am

    Excellent Anvesh Patel, this is pretty much useful

  30. Dheeraj
    June 26, 2018 at 9:10 am

    I have got very useful information on prepared statements

    Thank you Anvesh

  31. Mairc
    April 15, 2019 at 8:13 am

    This is really interesting, You’re a very skilled
    blogger. I have joined your rss feed and look forward to seeking
    more of your wonderful post.

  32. santosh kumar
    May 21, 2019 at 6:29 am

    How to create dynamically query in mysql for c#

  33. davidDox
    August 7, 2019 at 12:50 pm

    I positively liked entire lot that was written.
    I’d out of to abide by reading more and more.
    I will be happy to learn as much as I can
    I genuinely admire you since the follow-up so well done thank you very much for the time. Successfully!

  34. Alexey
    January 27, 2020 at 10:54 pm

    Thank you, brother. U r a great man.
    I just can’t understand why the internet is full of irrelevant info about MYSQL. Nobody says mysql doesn’t support dynamic SQL in stored procedure unless you have prepared statement.You really saved my life

    • Anvesh Patel
      Anvesh Patel
      May 27, 2020 at 12:28 pm

      Great…

Anvesh Patel
Anvesh Patel

Database Engineer

September 15, 2015 MySQLAnvesh Patel, database, database research and development, dbrnd, dynamic sql, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....