MySQL stored procedures-use of Process Control

Source: Internet
Author: User

#五. Use of Process Control

#1. IF Use

Create PROCEDURE Iftest1 ()
BEGIN
DECLARE a int DEFAULT 10;
--IF (a>1 &&a<20) Then
IF (a>1 and a<20) then
SELECT ' OK ';
ELSE SELECT ' No ';
END IF;
END;
Call Iftest ();

#2. Case use

Create PROCEDURE Casetest ()
BEGIN
DECLARE a int DEFAULT 10;
Case A
When a>10 then SELECT ' no ';
When ten then SELECT ' OK ';
ELSE SELECT ' Nono ';
End case;
END;
Call Casetest ();
#3. Loop use

CREATE PROCEDURE looptest ()
BEGIN
DECLARE a int DEFAULT 0;
DECLARE b int DEFAULT 0;
My_loop:loop
Set a=a+1;
If a>10 then LEAVE my_loop;
END if;
Set b=a;

END LOOP My_loop;
SELECT b;

End

Call Looptest ();

#4. LEAVE use jump out of the current loop to see the previous test case looptest equivalent to break in the Java loop

#5. Iterate use, jump to the loop head execution, equivalent to continue,iterate in the Java loop can only be used in the loop


CREATE PROCEDURE iteratetest ()
BEGIN
DECLARE a int DEFAULT 0;
DECLARE b int DEFAULT 0;
My_loop:loop
Set a=a+1;
If a>10 then LEAVE my_loop;
ELSEIF a>5 then iterate my_loop;
END if;
Set b=a;

END LOOP My_loop;
SELECT b;

End

Call Iteratetest ();

#6. REPEAT Loop with conditions

CREATE PROCEDURE repeattest ()
BEGIN
DECLARE a int DEFAULT 0;
REPEAT
Set a=a+1;
UNTIL a>10
END REPEAT;
SELECT A;
END;

Call Repeattest ();
#7. While with conditional loops

CREATE PROCEDURE whiletest ()
BEGIN
DECLARE a int DEFAULT 0;

While a<10 do
Set a=a+1;
END while;
SELECT A;

END;

Call Whiletest ()

MySQL stored procedures-use of Process Control

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.