Stored Procedure Programming for MySQL5 - Part 2
by Ligaya Turmelle
(2006-08-08)
Now that we've become familiar with the fundamentals of stored procedures it is time to start playing with the “Big Boy Toys”. This article will go over stored procedures's built in error handling, the security features available, various “extras” available, what isn't allowed in a stored procedure, and some basic administration of the stored procedures. So lets stop talking and bust open the toy box and start playing!
Let's jump right in with the sample stored procedure we will be working with. It isn't particularly elaborate but it does touch on just about everything we will be going over. So it will be a decent reference to the various sections we are talking about.
Sample Code
The CREATE statement for the table it will work with:
CREATE TABLE test (
ID INT AUTO_INCREMENT NOT NULL,
COL1 INT NOT NULL DEFAULT 0,
PRIMARY KEY (ID));
The stored procedure itself:
mysql> DROP PROCEDURE simpleMassInsert;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE simpleMassInsert(
-> IN myTable VARCHAR(30),
-> IN col VARCHAR(30),
-> IN value INT,
-> IN numRec INT,
-> OUT errMsg VARCHAR(80))
-> LANGUAGE SQL
-> NOT DETERMINISTIC
-> MODIFIES SQL DATA
-> SQL SECURITY INVOKER
-> COMMENT 'Inserts x number of records into a given table
and column with a given value '
-> BEGIN
-> # counter for our loop #
-> DECLARE count INT DEFAULT 0;
->
-> # basic error handling if table or column doesn't exist #
-> DECLARE unknown_column CONDITION FOR SQLSTATE '42S22';
-> DECLARE unknown_table CONDITION FOR SQLSTATE '42S02';
->
-> # the specific handlers #
-> DECLARE EXIT HANDLER FOR unknown_column
-> SET errMsg = 'Error: Column does not exist.';
->
-> DECLARE EXIT HANDLER FOR unknown_table
-> SET errMsg = 'Error: table does not exist.';
->
-> # Actual work being done #
-> SET errMsg = '';
-> WHILE count < numRec DO
-> # bit of a work around since #
-> # MySQL SP's don't like variable #
-> # interpolation in a query. So we build #
-> # the query, and use prepared statements #
-> SET @query = CONCAT('INSERT INTO ', myTable,'
(', col,') VALUES (?)');
-> PREPARE qry FROM @query;
-> SET @v = value;
-> # EXECUTE only works with #
-> # user variables in this context #
-> EXECUTE qry USING @v;
-> SET count = count + 1;
-> END WHILE;
-> END //
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
Verification the stored procedure works:
Note: Remember that when calling a stored procedure you must provide all the parameters specified by the stored procedure. So “test', ”COL1“, 3 and 5 will go to the IN vars of the stored procedure and the @error var will hold anything that comes out of the stored procedure.
mysql> CALL simpleMassInsert("test", "COL1", 3, 5, @error);
Query OK, 0 rows affected (0.14 sec)
mysql> SELECT @error;
+--------+
| @error |
+--------+
| |
+--------+
1 row IN SET (0.00 sec)
mysql> SELECT * FROM test;
+----+------+
| ID | COL1 |
+----+------+
| 16 | 3 |
| 17 | 3 |
| 18 | 3 |
| 19 | 3 |
| 20 | 3 |
+----+------+
5 rows IN SET (0.00 sec)