MySQL stored procedure loop Loop statement usage

Source: Internet
Author: User

There are three standard loops in the statements of the MySQL stored procedure: The while loop, the loop loop, and the repeat loop. There is also a non-standard way of cycling: GOTO, but this cycle is best not to use, it is easy to cause confusion in the program, here is a good introduction.

Today we'll take a look at the loop loop.

MySQL Tutorials >
Mysql> delimiter $$
Mysql>
Mysql> CREATE PROCEDURE MyProc ()
-> BEGIN
->
-> DECLARE I int;
-> SET i=0;
-> Loop1:loop
-> SET i=i+1;
-> IF i>=10 THEN/*last number-exit loop*/
-> LEAVE Loop1;
-> ELSEIF MOD (i,2) =0 THEN/*even number-try again*/
-> iterate Loop1;
-> End IF;
->
-> SELECT CONCAT (i, "is a odd number");
->
-> End LOOP Loop1;
->
->
-> end$$
Query OK, 0 rows affected (0.02 sec)

Mysql>
Mysql> delimiter;
Mysql> call MyProc ();
+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 1 is a odd number |
+-------------------------------+
1 row in Set (0.00 sec)

+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 3 is a odd number |
+-------------------------------+
1 row in Set (0.00 sec)

+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 5 is a odd number |
+-------------------------------+
1 row in Set (0.00 sec)

+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 7 is a odd number |
+-------------------------------+
1 row in Set (0.01 sec)

+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 9 is a odd number |
+-------------------------------+
1 row in Set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Mysql> drop procedure MyProc;
Query OK, 0 rows Affected (0.00 sec)

Mysql>

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.