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

Stored Procedure Programming for MySQL5 - Part 1

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


 

A Better Example

This is a slightly more advanced example then last one. This stored procedure will take take in and pass out a variable and do some simple logic amongst other things. Nothing to hard - but something that will hopefully expand your mind a bit to the possibilities and power of stored procedures.

  1. mysql> DROP PROCEDURE statusOfDay;
  2. Query OK, 0 rows affected, 3 warnings (0.00 sec)
  3.  
  4. mysql> DELIMITER //
  5. mysql> CREATE PROCEDURE statusOfDay
  6.      (IN inVar VARCHAR(10), OUT outVar VARCHAR(30))
  7.     -> BEGIN
  8.     ->   IF(inVar = "Woohoo") THEN
  9.     ->     SET outVar = "Today is a good day.";
  10.     ->   ELSE
  11.     ->     SET outVar = "Nothing to chear about today.";
  12.     ->   END IF;
  13.   -> END //
  14. Query OK, 0 rows affected (0.00 sec)
  15.  
  16. mysql> DELIMITER ;
This example has a number of things that were not in the last example, so lets again break it down to see what each part does.

  1. mysql> DROP PROCEDURE statusOfDay;
I personally like to drop the procedure with the same name as the procedure I am about to create. Saves me from any name clashes when I create the new procedure. Granted I have to be very careful not to accidentally drop a procedure created earlier (by me or someone else) with the same name that may or may not do something completely different then the stored procedure I am currently making... but I should have checked that BEFORE I got ready to write (never mind run) the SQL to create a new stored procedure.

  1. mysql> DELIMITER //
  2. ...
  3. mysql> DELIMITER ;
The DELIMITER command is a mysql client command that allows the user to change the statement delimiter. Since a stored procedure can be (and in this case is) a series of mysql SQL commands - when we are creating one we have to be able to place multiple SQL statements in the mysql client without it “running”. By using DELIMITER we can change the statement delimiter to something other then the “;”. A very quick example that changes the delimiter from a semi-colon to a double slash and back to a semi-colon.

  1. mysql> delimiter //
  2.  
  3. mysql>SELECT COUNT(*) FROM t//
  4. Query OK, 0 rows affected (0.00 sec)
  5.  
  6. mysql> delimiter ;

 


 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