 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How MySQL REPEAT loop statement can be used in stored procedure?
As we know that MySQL provides us loop statements that allow us to execute a block of SQL code repeatedly based on a condition. A REPEAT loop statement is one of such kind of loop statement. Its syntax is as follows −
REPEAT statements; UNTIL expression END REPEAT
First of all, MySQL executes the statements, and then it evaluates the expression. If the expression evaluates to FALSE, MySQL executes the statements repeatedly until the expression evaluates to TRUE. The REPEAT loop checks the expression after the statements execute, that is why it is also called a post-test loop.
To demonstrate the use of a REPEAT loop with stored procedures, the following is an example −
mysql> Delimiter // mysql> CREATE PROCEDURE Repeat_Loop() -> BEGIN -> DECLARE A INT; -> DECLARE XYZ Varchar(50); -> SET A = 1; -> SET XYZ = ''; -> REPEAT -> SET XYZ = CONCAT(XYZ,A,','); -> SET A = A + 1; -> UNTIL A > 10 -> END REPEAT; -> SELECT XYZ; -> END // Query OK, 0 rows affected (0.04 sec)
Now, we can see the result below when we invoke this procedure −
mysql> DELIMITER ; mysql> CALL Repeat_Loop(); +-----------------------+ | XYZ | +-----------------------+ | 1,2,3,4,5,6,7,8,9,10, | +-----------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
Advertisements
 