Art AboutFAQWrite for usContact InfoRSS Feed
Search A/R/T:

Stored Procedure Programming for MySQL5 - Part 1

by Ligaya Turmelle — Page 2 of 5
Click to rate:    
216 votes / avg. rating 36.33%


 

Background Information

Now we could just jump into stored procedures here - but I wish to start by talking about a few things that you may or may not know about. All of these subjects are related to stored procedures even though they aren't necessary to create a stored procedure.

Many people do not realize that MySQL has a number of logic flow and looping structures available for both inside and outside a stored procedure. I, of course, will be using the stored procedure “types” in my examples but I won't get very deep into them since it is assumed you know the basics of programming logic already. I definitely want to make sure you have a quick overveiw though. Be sure to read the manual pages if you have any questions.

MySQL has the standard IF statement with ELSEIF and ELSE support as well as the CASE statement (logical equivalent to the PHP “switch” statement). These, of course, control any logical decisions to be made in the stored procedures. Both use a different syntax then PHP so be sure to check out their manual pages for the exact syntax.

MySQL has 3 looping structures. They include the Labeled LOOP, the REPEAT statement (equivalent to a do-while), and the WHILE statement. Along with these looping structures you have the LEAVE and ITERATE statements which are equivalent to PHP's break and continue statements. The looping allows you to easily control repetitive operations, but you already knew that. Again check out their manual pages if you are unfamiliar with their syntax.

MySQL does allow the creation of user defined variables both inside and outside stored procedures. Outside of stored procedures you can create variables with the SET keyword. Inside the stored procedure you can use the DECLARE statement for locally scoped variables as well as a SET statement (an extended version of the general SET). Being able to create and manipulate variables is a wonderful thing after all.

  1. mysql> SET @a = "This is a test of the emergency
  2.                  broadcasting system";
  3. Query OK, 0 rows affected (0.06 sec)
  4.  
  5. mysql> SELECT @a;
  6. +-----------------------------------------------------+
  7. | @a                                                  |
  8. +-----------------------------------------------------+
  9. | This IS a test of the emergency broadcasting system |
  10. +-----------------------------------------------------+
  11. 1 row IN SET (0.00 sec)
MySQL has a very fine grained permission system built in. To see if you have been given the proper permissions to create a procedure you can type in

  1. SHOW GRANTS FOR CURRENT_USER;
Check to see if “CREATE ROUTINE” is listed. If it is - you are golden. If not - then we need to give you the proper permissions. Otherwise you will be sitting in that chair with nothing to do but read - boring.

Now without getting too into it - log in to mysql as a user that has the GRANT priveledge on the database you will be working on (worst case scenerio - root, though that should be used only after all other users have been tried. Then be very careful.) and type in

  1. mysql>GRANT CREATE ROUTINE ON DATABASE
  2.       TO 'user'@'host' IDENTIFIED BY 'password';
Fill in database, user, host and password with your appropriate values. For more information on GRANT and MySQL permissions please see their manual pages.

 


 Tags:  , , , , ,
 Add tag(s) (comma separated):
[Tags beta] — [Add New][Help]  

Tags Help

Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.

To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.

New Comment
This form allows you to type in a new comment. Keep in mind the following:
  • The system accepts input in plain text format. Newlines will be converted to the HTML equivalent, and the system will try to catch most URLs and make them clickable.
  • Your e-mail address will never be displayed. We will use it only to notify you when new comments are posted to this page.
  • As a rule, we do not delete comments unless they are offensive, racist, spam or otherwise inappropriate.
  • Bold fields are required
Your Name:
Your e-mail:
Type this number:
Subject:
Comment:
Comments   New Comment
Re: Stored Procedure Programming for MySQL5 - Part 1 (#208)
By Kasper A. Svendsen on 2006-07-12 12:34:04

Very nice article, good with the examples and so on! :=)

[Reply to this]

Re: Re: Stored Procedure Programming for MySQL5 - Part 1 (#341)
By khalid husain on 2006-10-09 04:10:38

kindly give me example of stored procedure

thanks

[Reply to this]

Re: Re: Stored Procedure Programming for MySQL5 - Part 1 (#524)
By Abhinav on 2007-04-23 02:13:30

n i c e

A r T i c L e

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#209)
By jcerdan on 2006-07-12 14:14:40

Users should be careful as the exact command is:

GRANT CREATE ROUTINE ON 'database'.* TO 'user'@'host' IDENTIFIED BY 'password';

Don't forget the .* after the database name.

[Reply to this]

Print (#211)
By Sasa on 2006-07-13 13:57:50

Where is the print version?

[Reply to this]

Re: Print (#222)
By Michael on 2006-07-18 02:40:24

Yep, a print version would be very nice.

[Reply to this]

Re: Re: Print (#378)
By userbiasa on 2006-12-14 12:26:32

Yes, I think putting printed version is a fair play after users browsing each pages. You can put it on the latest installment of these pages.

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#257)
By Jad on 2006-07-29 06:50:46

mysql> CALL RateOfArticle("Well Done", @a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @a;
-------------------------------
| @a |
-------------------------------
| Thank you Ligaya. |
-------------------------------
1 row IN SET (0.00 sec)

[Reply to this]

Very Very Thnx (#289)
By Sai Suman on 2006-08-17 02:52:56

Really Helps a lot for new Commers.. Thnx for u'r Support can u give u'r personal E-mail id so that i can clear my doubts in future

mysql> select @a;
---------------------
| @a |
---------------------
| Today is a Good Day |
---------------------
1 row in set (0.00 sec)

[Reply to this]

Re: Can you explani in brief (#558)
By priyank sharma on 2007-06-23 02:27:27

hi realy it is good blog but there is little bit of confusion for new commers . can you explain it(Stored Procedure Programming) in brief....

Many Thanks

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#476)
By Asmita on 2007-01-19 06:00:12

this page gives good and proper tutorial about stored procedure.but i think 2 examples are not sufficient and some more complex and realist examples should be added if possible

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#589)
By d on 2007-08-31 22:48:20

what web scripting languages support mysql5 stored procedures?

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#616)
By Patrick on 2007-12-08 08:57:17

Thank You!

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#623)
By php on 2008-01-13 17:54:50

how to use php to call stored procs in mysql 5? give example plz

[Reply to this]

Re: Re: Stored Procedure Programming for MySQL5 - Part 1 (#628)
By Sudhir Kumar Panda on 2008-03-14 07:11:47

Plz give me the reply

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#624)
By joseph mendez on 2008-02-13 20:43:57

i got error : ERROR 1064(42000): it says "You have an error in your syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PROCEDURE ....

[Reply to this]

Index