A2-06-09.MySQL Loop in Stored Procedures

转载自:http://www.mysqltutorial.org/stored-procedures-loop.aspx

MySQL Loop in Stored Procedures

 

Summary: in this tutorial, you will learn how to use various MySQL loop statements including WHILEREPEAT and LOOPto run a block of code repeatedly based on a condition.

MySQL provides loop statements that allow you to execute a block of SQL code repeatedly based on a condition. There are three loop statements in MySQL: WHILEREPEAT and LOOP.

We will examine each loop statement in more detail in the following sections.

WHILE loop

The syntax of the WHILEstatement is as follows:

The WHILE loop checks the expressionat the beginning of each iteration. If the expressionevaluates to TRUE, MySQL will execute statementsbetween WHILEand END WHILE until the expressionevaluates to FALSE. The WHILE loop is called pretest loop because it checks the expression before the statements execute.

The following flowchart illustrates the WHILEloop statement:

mysql while loop

 

Here is an example of using the WHILE loop statement in a stored procedure:

In the test_mysql_while_loopstored procedure above:

  • First, we build str string repeatedly until the value of the x variable is greater than 5.
  • Then, we display the final string using the SELECT statement.

Notice that if we don’t initialize the xvariable, its default value is NULL. Therefore, the condition in the WHILEloop statement is always TRUE and you will have an indefinite loop, which is not expected.

Let’s test the test_mysql_while_loopstored procedure:

mysql loop example

REPEAT loop

The syntax of the REPEAT loop statement is as follows:

First, MySQL executes the statements, and then it evaluates the expression. If the expressionevaluates to FALSE, MySQL executes the statements repeatedly until the expression evaluates to TRUE.

Because the REPEAT loop statement checks the expression after the execution of statements, the REPEATloop statement is also known as the post-test loop.

The following flowchart illustrates the REPEATloop statement:

mysql repeat loop

We can rewrite the test_mysql_while_loopstored procedure that uses WHILE loop statement above using the REPEAT loop statement:

It is noted that there is no semicolon (;) in the UNTIL expression.

 

LOOP, LEAVE and ITERATE statements

There are two statements that allow you to control the loop:

  • The LEAVEstatement allows you to exit the loop immediately without waiting for checking the condition. The LEAVEstatement works like the  break statement in other languages such as PHP, C/C++, and Java.
  • The ITERATEstatement allows you to skip the entire code under it and start a new iteration. The ITERATEstatement is similar to the continue statement in PHP, C/C++, and Java.

MySQL also gives you a LOOPstatement that executes a block of code repeatedly with an additional flexibility of using a loop label.

The following is an example of using the LOOP loop statement:

In this example,

  • The stored procedure only constructs a string with even numbers e.g., 2, 4, and 6.
  • We put a loop_label  loop label before the LOOPstatement.
  • If the value of  x is greater than 10, the loop is terminated because of the LEAVEstatement.
  • If the value of the x is an odd number, the ITERATE statement ignores everything below it and starts a new iteration.
  • If the value of the x is an even number, the block in the ELSEstatement will build the string with even numbers.

In this tutorial, you have learned various MySQL loop statements that execute a block of code repeatedly based on a condition.

 

posted @ 2018-08-28 15:33  zhuntidaoren  阅读(405)  评论(0)    收藏  举报