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

Stored Procedure Programming for MySQL5 - Part 1

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


 

  1. mysql> CREATE PROCEDURE
  2.      statusOfDay(IN inVar VARCHAR(10), OUT outVar VARCHAR(30))
Note the IN/OUT parameters in our create statement. This is how you pass in variables to the stored procedure as well as get some data out of it. There are 3 types - IN (data going in to the SP), OUT (data coming out of the SP) and INOUT (data that goes in, is (hopefully) manipulated and then is sent back out). In this case we have a variable going in (inVar VARCHAR(10)) and a variable coming out (outVar VARCHAR(30)). It is important to note that we have to not only provide a name for these variables but also provide the datatype for the variable. Using the wrong datatype can and will generate the wrong output if not an error when you use the stored procedure.

  1. -> BEGIN
  2.     ->   IF(inVar = "Woohoo") THEN
  3.     ->     SET outVar = "Today is a good day.";
  4.     ->   ELSE
  5.     ->     SET outVar = "Nothing to cheer about today.";
  6.     ->   END IF;
  7.   -> END //
  8. Query OK, 0 rows affected (0.00 sec)
This time our stored procedure consists of more then 1 line of SQL (the IF statement consists of multiple lines). To compensate for that we use the <a href=“#Delimiter”>DELIMITER</a> and the BEGIN/END statements. The BEGIN and END statements clearly mark where the stored procedure's body of SQL statements begin and end. Two things to pay attention to:

  • that although we have altered the DELIMITER for the creation of the stored procedure, we still use the semi-colan as the statement ending between the BEGIN and END statements.
  • that the character(s) used in the DELIMITER are placed on the end of the line that contains the END statement.
  1. mysql> CALL statusOfDay("Woohoo", @a);
  2. Query OK, 0 rows affected, 2 warnings (0.02 sec)
Just like calling a user function in PHP - you can call a stored procedure in MySQL. If you have arguements to pass to the stored procedures parameters - there needs to be a value. “Woohoo” is passed to the statusOfDay stored procedure and the @a variable is loaded with the value that is passed out of the stored procedure. Notice that the OUT variable was NOT displayed when we called the stored procedure.

So how do we get a hold of the value that was passed out of the stored procedure? Simple, we access the @a user variable in our SQL. Simply SELECT it.

  1. mysql> SELECT @a;
  2. +----------------------+
  3. | @a                   |
  4. +----------------------+
  5. | Today IS a good day. |
  6. +----------------------+
  7. 1 row IN SET (0.00 sec)
I personally like to check the functionality of my scripts with a little testing to make sure I get out what I am expecting. Here I use the statusOfDay stored procedure in a way to get the ELSE side of the IF statement. This way I know that both sides of the IF statement work.

  1. mysql> CALL statusOfDay("Yuk", @a);
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> SELECT @a;
  5. +-------------------------------+
  6. | @a                            |
  7. +-------------------------------+
  8. | Nothing TO cheer about today. |
  9. +-------------------------------+
  10. 1 row IN SET (0.00 sec)

Conclusion

Well there you have it - an overview of programming stored procedures for MySQL 5. With an understanding of the Basic Concepts and the line by line explaination of the examples, you should be ready to write a few of your own stored procedures. So go out there and put that database to work!

Please wait for the ride to come to a full and complete stop before exiting the vehicle. Thank you for riding Part 1 of the Stored Procedure ride and we hope to see you again at Part 2. Have a Great Day.

Ligaya Turmelle is a full time Goddess, occasional PHP programmer, and obsessive world traveler. She currently lives in Guam with her husband and daughter and their 2 Belgian Malinois. Actively involved with the PHP Community she hopes to one day actually meet the people she talks to.

 


 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