Getting started with MySQL 5.0 Stored Procedure Programming

Source: Internet
Author: User

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:

 
 
  1. CREATE  
  2. [DEFINER = { user | CURRENT_USER }]  
  3. PROCEDURE sp_name ([proc_parameter[,...]])  
  4. [characteristic ...] routine_body  
  5. proc_parameter:  
  6. [ IN | OUT | INOUT ] param_name type  
  7. type:  
  8. Any valid MySQL data type  
  9. characteristic:  
  10. LANGUAGE SQL  
  11. | [NOT] DETERMINISTIC  
  12. | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }  
  13. | SQL SECURITY { DEFINER | INVOKER }  
  14. | COMMENT 'string'  
  15. routine_body:  
  16. 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:

 
 
  1. CREATE PROCEDURE phelloword()  
  2. BEGIN  
  3. SELECT 'Hello Word!' AS F;  
  4. END;  

Copy the statement used to create the phelloword stored procedure to phpMyAdmin and run the command. The following error is returned:

 
 
  1. #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:

 
 
  1. CREATE PROCEDURE ptest()  
  2. BEGIN  
  3. SELECT 'Hello Word!' AS F;  
  4. 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:

 
 
  1. CREATE PROCEDURE plogin  
  2. (  
  3. p_username char(15),  
  4. p_password char(32),  
  5. p_ip char(18),  
  6. p_logintime datetime  
  7. )  
  8. LABEL_PROC:  
  9. BEGIN   
  10. DECLARE v_uid mediumint(8);   
  11. DECLARE v_realpassword char(32);   
  12. DECLARE v_nickname varchar(30);   
  13. DECLARE v_oltime smallint(6);   
  14. SELECT u.uid, u.password, f.nickname, u.oltime INTO v_uid, v_realpassword, v_nickname, v_oltime  
  15. FROM cdb_members u INNER JOIN cdb_memberfields f ON f.uid = u.uid WHERE u.username = p_username;   
  16. IF (v_uid IS NULL) THEN  
  17. SELECT 2 AS ErrorCode;  
  18. LEAVE LABEL_PROC;END IF;  
  19. IF (p_password <> v_realpassword) THEN  
  20. SELECT 3 AS ErrorCode;  
  21. LEAVE LABEL_PROC;  
  22. END IF;  
  23. UPDATE ipsp_userexpands SET lastloginip = p_ip, lastlogintime = p_logintime WHERE uid = v_uid;  
  24. SELECT 0 AS ErrorCode, v_uid AS uid, v_nickname AS nickname, v_oltime AS oltime;  
  25. 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:

 
 
  1. IF ... THEN  
  2. ...;  
  3. ELSE  
  4. IF ... THEN  
  5. ...;  
  6. ELSEIF  
  7. ...;  
  8. ELSE  
  9. ...;  
  10. END IF;  
  11. 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.

 
 
  1. CREATE PROCEDURE ipsp_getresourcedir  
  2. (  
  3. p_hashcode char(40)  
  4. )  
  5. LABEL_PROC:  
  6. BEGIN  
  7. DECLARE v_sql varchar(200);  
  8. SET v_sql = CONCAT('SELECT filedir FROM ipsp_resources WHERE hashcode =\'', p_hashcode, '\' LIMIT 0, 1');  
  9. SET @sql = v_sql;  
  10. PREPARE sl FROM @sql;  
  11. EXECUTE sl;  
  12. DEALLOCATE PREPARE sl;  
  13. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.