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

Stored Procedure Programming for MySQL5 - Part 1

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


 

Stored Procedures

Now for the nitty gritty. Time to get started with stored procedures. Go to the database you will be learning in and follow along.

First we will create a simple example (Hello World of course - I firmly believe in tradition), then we will break it down into it's various parts.

  1. mysql> CREATE PROCEDURE HelloWorld()
  2.     -> SELECT 'Hello World';
  3. Query OK, 0 rows affected (0.08 sec)
Ok. So what did I do, and why does that kinda look familiar? Lets break it down.

  1. mysql> CREATE PROCEDURE HelloWorld()
Here we are using the 'CREATE PROCEDURE' keywords to make a procedure named HelloWorld() in the current database. Note the empty parenthesis behind HelloWorld. Like a PHP function those are required. If we were passing in data or passing out data - that is where we would declare it. Looks kinda like what we do when we make a user defined function in PHP.

  1. -> SELECT 'Hello World';
The next line is where we placed our SQL statement. In this case it is a simple SELECT with a string. Note the semi-colon at the end of the line. That submits the SQL to MySQL... Anyway - nothing scary here.. moving right along.

  1. Query OK, 0 rows affected (0.08 sec)
Well there you go. The query ran ok - and you created your first stored procedure. Congratulations! Flex those coding muscles.

Feeling proud of yourself? Wait... You want to use it now?! Greedy lot aren't you. Very well, let's use our stored procedure to display “Hello World”...

  1. mysql> CALL HelloWorld();
  2. +-------------+
  3. | Hello World |
  4. +-------------+
  5. | Hello World |
  6. +-------------+
  7. 1 row IN SET (0.00 sec)
  8.  
  9. Query OK, 0 rows affected (0.00 sec)
Did you notice we used the SQL keyword CALL along with the stored procedure's name? And let's not forget the parenthesis after the procedures name. Pretty easy isn't it - almost insultingly easy. Ready to try something a bit harder?

Good. Again - I am assuming you already know basic programming logic. I won't be explaining that as we go along, so if you don't understand it - go back and read the manual pages provided in the Background Information section.

 


 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