This article mainly describes how to program the MySQL 5.0 stored procedure. We started it on MySQL version 5.0.45 phpMyAdmin and version 2.11.3, first, let's look at the syntax for creating a stored procedure in MySQL 5.0 reference manual:
- CREATE
- [DEFINER = { user | CURRENT_USER }]
- PROCEDURE sp_name ([proc_parameter[,...]])
- [characteristic ...] routine_body
- proc_parameter:
- [ IN | OUT | INOUT ] param_name type
- type:
- Any valid MySQL data type
- characteristic:
- LANGUAGE SQL
- | [NOT] DETERMINISTIC
- | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
- | SQL SECURITY { DEFINER | INVOKER }
- | COMMENT 'string'
- routine_body:
- Valid SQL procedure statement
If you are not familiar with MySQL, simply reading this syntax structure is certainly not enough for MySQL Stored Procedure programming. I used ms SQL server before, so the following records my familiarity with the MySQL stored procedure are also a major difference between ms SQL SERVER and MySQL.
The first step is to write a Hello Word stored procedure as follows:
- CREATE PROCEDURE phelloword()
- BEGIN
- SELECT 'Hello Word!' AS F;
- END;
Copy the statement used to create the phelloword stored procedure to phpMyAdmin and run the command. The following error is returned:
- #1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
I have been entangled for a long time in this issue. The execution in MySQL command line tool is also not successful, but according to the prompt information, we can know that the execution in SELECT 'Hello Word! 'As F; ends, followed by END; not executed, which obviously leads to errors.
Select a separator. The syntax is as follows: DELIMITER //
The Delimiter is the symbol that notifies the MySQL client that the input is complete. ";" Is always used, but not in the MySQL 5.0 stored procedure, because many statements in the Stored Procedure require semicolons.
Therefore, the above stored procedure is changed:
- CREATE PROCEDURE ptest()
- BEGIN
- SELECT 'Hello Word!' AS F;
- END //
In addition, when executing in phpMyAdmin, enter // In the Delimiter text box. This stored procedure can be created successfully.
Step 2: Write a complete stored procedure that includes parameters, variables, variable assignment, condition judgment, UPDATE statements, and SELECT returned result set, as follows:
- CREATE PROCEDURE plogin
- (
- p_username char(15),
- p_password char(32),
- p_ip char(18),
- p_logintime datetime
- )
- LABEL_PROC:
- BEGIN
- DECLARE v_uid mediumint(8);
- DECLARE v_realpassword char(32);
- DECLARE v_nickname varchar(30);
- DECLARE v_oltime smallint(6);
- SELECT u.uid, u.password, f.nickname, u.oltime INTO v_uid, v_realpassword, v_nickname, v_oltime
- FROM cdb_members u INNER JOIN cdb_memberfields f ON f.uid = u.uid WHERE u.username = p_username;
- IF (v_uid IS NULL) THEN
- SELECT 2 AS ErrorCode;
- LEAVE LABEL_PROC;END IF;
- IF (p_password <> v_realpassword) THEN
- SELECT 3 AS ErrorCode;
- LEAVE LABEL_PROC;
- END IF;
- UPDATE ipsp_userexpands SET lastloginip = p_ip, lastlogintime = p_logintime WHERE uid = v_uid;
- SELECT 0 AS ErrorCode, v_uid AS uid, v_nickname AS nickname, v_oltime AS oltime;
- END LABEL_PROC //
The first thing to talk about is the syntax for assigning values to variables. MySQL uses SELECT u. uid, u. password, f. nickname, u. oltime INTO v_uid, v_realpassword, v_nickname, v_oltime FROM cdb_members u inner join cdb_memberfields f ON f. uid = u. uid WHERE u. username = p_username; assign a value to the variable.
The second is the syntax structure of condition judgment, as shown below:
- IF ... THEN
- ...;
- ELSE
- IF ... THEN
- ...;
- ELSEIF
- ...;
- ELSE
- ...;
- END IF;
- END IF;
Finally, let's talk about the use of LEAVE syntax. When a certain condition is met and the following SQL statement is not executed, the RETURN syntax is used in ms SQL SERVER. in MySQL, the corresponding keyword is not found, however, the LEAVE syntax can be used to meet the requirements. Define a label before the BEGIN of the MySQL 5.0 stored procedure, for example, "LABEL_PROC: "and then execute" LEAVE LABEL_PROC; "where RETURN is interrupted.
Step 3: Create a stored procedure for executing dynamic SQL statements.
- CREATE PROCEDURE ipsp_getresourcedir
- (
- p_hashcode char(40)
- )
- LABEL_PROC:
- BEGIN
- DECLARE v_sql varchar(200);
- SET v_sql = CONCAT('SELECT filedir FROM ipsp_resources WHERE hashcode =\'', p_hashcode, '\' LIMIT 0, 1');
- SET @sql = v_sql;
- PREPARE sl FROM @sql;
- EXECUTE sl;
- DEALLOCATE PREPARE sl;
- END LABEL_PROC //
Here we mention that "\" is an escape character. The concatenated SQL statement is similar to SELECT filedir FROM ipsp_resources WHERE hashcode = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' LIMIT 0, 1
In addition, @ SQL is a user variable. For detailed usage, see the MySQL reference manual.
If you have experience writing stored procedures on ms SQL server, after reading these, I think the basic MySQL Stored Procedure programming should be able to cope with it!
For more information, see the MySQL reference manual or related books!
Keywords: MySQL, Stored Procedure, Stored Procedure, Xiaoqiang, hsqzzzl, http://hi.baidu.com/hsqzzzl
Original article title: Theme: MySQL 5.0 Stored Procedure Programming
Connection: http://www.cnblogs.com/sunwei2012/archive/2010/03/11/1683889.html