MySQL version: 5.0.45 phpMyAdmin version: 2.11.3
First, read the syntax for creating a stored procedure in MySQL 5.0 reference manual:
CREATE
[DEFINER = {user
| CURRENT_USER}]
PROCEDUREsp_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 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 stored procedure, for example, "LABEL_PROC:", and then run "LEAVE LABEL_PROC" where the 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!