
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How Can MySQL LOOP statement be used in a stored procedure?
MySQL provides us a LOOP statement that executes a block of code repeatedly along with an added flexibility of using a loop label. We have the following two statements that allow us to control the loop −
LEAVE statement
It allows us to exit the loop immediately without waiting for checking the condition.
Iterate statement
It allows us to skip the entire code under it and start a new iteration.
To demonstrate the use of LOOP statement with stored procedures, the following is a stored procedure which constructs a string with even numbers like 2,4,6,8 etc. −
mysql> Delimiter // mysql> CREATE PROCEDURE LOOP_loop() -> BEGIN -> DECLARE A INT; -> DECLARE XYZ VARCHAR(255); -> SET A = 1; -> SET XYZ = ''; -> loop_label: LOOP -> IF A > 10 THEN -> LEAVE loop_label; -> END IF; -> SET A = A + 1; -> IF (A mod 2) THEN -> ITERATE loop_label; -> ELSE -> SET XYZ = CONCAT(XYZ,A,','); -> END IF; -> END LOOP; -> SELECT XYZ; -> END // Query OK, 0 rows affected (0.07 sec)
Now, we can see the result below when we invoke this procedure −
mysql> DELIMITER ; mysql> CALL LOOP_loop (); +-------------+ | XYZ | +-------------+ | 2,4,6,8,10, | +-------------+ 1 row in set (0.04 sec) Query OK, 0 rows affected (0.04 sec)
Here in the above query, if the value of A is greater than 10 then the loop terminated because of the LEAVE statement. If the value of A is an odd number then the ITERATE statement ignores everything below it and starts a new iteration. If the value of A is an even number then the block in the ELSE statement will build the string with even numbers.
- Related Articles
- How MySQL WHILE loop statement can be used in stored procedure?
- How MySQL REPEAT loop statement can be used in stored procedure?
- How MySQL IF statement can be used in a stored procedure?
- How Can MySQL CASE statement be used in stored procedure?
- How MySQL IF ELSE statement can be used in a stored procedure?
- How can MySQL IF ELSEIF ELSE statement be used in a stored procedure?
- How can local variables be used in MySQL stored procedure?
- How can user variables be used in MySQL stored procedure?
- How to loop thrugh a stored procedure in MySQL?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How to use FOR LOOP in MySQL Stored Procedure?
- How to call a stored procedure using select statement in MySQL?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- How can we invoke MySQL stored procedure?
