Summary of cycles in MySQL stored procedures and stored procedures

Source: Internet
Author: User

Directly add content

First, the CASE conditions in the Stored Procedure

 
 
  1. CREATE PROCEDURE p_case(IN parameter1 INT) 
  2. BEGIN 
  3.     DECLARE variable1 INT; 
  4.     SET variable1 = parameter1 + 1; 
  5.     CASE variable1 
  6.         WHEN 0 THEN INSERT INTO t VALUES(0); 
  7.         WHEN 1 THEN INSERT INTO t VALUES(1); 
  8.         ELSE INSERT INTO t VALUES(2); 
  9.     END CASE; 
  10. END; 

LOOPS Loop
[1] WHILE... END WHILE
[2] LOOP... END LOOP
[3] REPEAT... END REPEAT
【4】 GOTO

I. WHILE... END WHILE

 
 
  1. CREATE PROCEDURE p_while() 
  2. BEGIN 
  3.     DECLARE v INT; 
  4.     SET v = 0; 
  5.     WHILE v < 5 DO 
  6.         INSERT INTO t VALUES(v); 
  7.         SET v = v + 1; 
  8.     END WHILE; 
  9. END;// 

2. LOOP... END LOOP

 
 
  1. CREATE PROCEDURE p_loop() 
  2. BEGIN 
  3.     DECLARE v INT; 
  4.     SET v = 0; 
  5.     loop_label:LOOP 
  6.         INSERT INTO t VALUES(v); 
  7.         SET v = v + 1; 
  8.         IF v>=5 THEN 
  9.             LEAVE loop_label; 
  10.         END IF; 
  11.     END LOOP; 
  12. END;//  

Iii. REPEAT... END REPEAT
 

 
 
  1. CREATE PROCEDURE p_repeat() 
  2. BEGIN 
  3.     DECLARE v INT; 
  4.     SET v = 0; 
  5.     REPEAT 
  6.         INSERT INTO t VALUES(v); 
  7.         SET v = v + 1; 
  8.         UNTIL v>=5 
  9.     END REPEAT; 
  10. END;// 

Stored Procedure:

Returns

 
 
  1. CREATE PROCEDURE optionsrank( 
  2.     OUT pl INT, 
  3.     OUT ph INT, 
  4.     OUT pa INT 
  5. BEGIN 
  6.     SELECT MAX(option_id) INTO ph FROM wp_options; 
  7.     SELECT Min(option_id) INTO pl FROM wp_options; 
  8.     SELECT AVG(option_id) INTO pa FROM wp_options; 
  9. END; 
  10. CALL optionsrank(@pl,@ph,@pa) // 
  11. SELECT @pl,@ph,@pa // 

In and out have input AND output parameters.

 
 
  1. CREATE PROCEDURE in_out_test( 
  2.     IN in_option_id INT, 
  3.     OUT out_option_value TEXT 
  4. BEGIN  
  5.     SELECT option_value INTO out_option_value FROM wp_options WHERE option_id = in_option_id; 
  6. END; 
  7. CALL in_out_test(100,@out) // 
  8. SELECT @out // 

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.