Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn SQL using MySQL in One Day and Learn It Well

You're reading from  Learn SQL using MySQL in One Day and Learn It Well

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781836205678
Pages 121 pages
Edition 1st Edition
Languages
Author (1):
Jamie Chan Jamie Chan
Profile icon Jamie Chan

REPEAT statement

 

Next, let’s look at the REPEAT statement. A REPEAT statement is also used to perform repetitive tasks.

 

It repeatedly performs some tasks until the UNTIL condition is met. The syntax of a REPEAT statement is:

 

[name of repeat statement :] REPEAT

-- do some tasks

UNTIL stop condition is met

END REPEAT;

 

Example

 

DELIMITER $$

CREATE FUNCTION repeat_demo(x INT, y INT) RETURNS VARCHAR(255) DETERMINISTIC

BEGIN

DECLARE z VARCHAR(255);

SET z = '';

REPEAT

      SET x = x + 1;

      SET z = concat(z, x);

      UNTIL x>=y

END REPEAT;

RETURN z;

END $$

DELIMITER ;

 

This REPEAT statement repeats two tasks (SET x = x + 1 and SET z = concat(z, x)) until the x >= y condition is met.

 

If you run the function with the following statement

 

SELECT repeat_demo(1, 5);

 

You’...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}