Stored Procedure Programming for MySQL5 - Part 1
|
|
|
Click to rate: |
|
|
|
216 votes / avg. rating 36.33%
|
|
Background InformationNow 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.
mysql> SET @a = "This is a test of the emergency
broadcasting system";
Query OK, 0 rows affected (0.06 sec)
mysql> SELECT @a;
+-----------------------------------------------------+
| @a |
+-----------------------------------------------------+
| This IS a test of the emergency broadcasting system |
+-----------------------------------------------------+
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
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
mysql>GRANT CREATE ROUTINE ON DATABASE
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:
mysql, procedure, d, storedprocedure, programming, ghdf
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.
|
|
 |
|
 |
|