Loops
Loops allow stored programs to execute statements repetitively. The MySQL stored program language offers three types of loops :
Simple loops using the
LOOP
andEND LOOP
clausesLoops that continue while a condition is true, using the
WHILE
andEND WHILE
clausesLoops that continue until a condition is true, using the
REPEAT
andUNTIL
clauses
With all three loop types, you terminate execution of the loop
with the LEAVE
statement.
All three types of loops are described in detail in Chapter 4; we’ll only demonstrate the
LOOP-LEAVE-END LOOP
(simple loop)
sequence in this tutorial.
Figure 2-9 shows a very simple loop.
Here is an explanation of the activity in this stored procedure:
Line(s) | Explanation |
7 | Declare a simple numeric variable
called |
9-14 | The simple loop. All statements
between |
9 | The |
10 | Increment the |
11-13 | Test for the value of |
15 | We proudly announce that we can count to 10! |
Get MySQL Stored Procedure Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.