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

WHILE statement

 

The next control flow statement is the WHILE statement. This statement allows us to specify a task to be done repeatedly while a certain condition is valid.

 

The syntax is:

 

[name of while statement : ] WHILE condition is true DO

-- some tasks

END WHILE;

 

Example

 

DELIMITER $$

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

BEGIN

DECLARE z VARCHAR(255);

SET z = '';

      

while_example: WHILE x<y DO

      SET x = x + 1;

      SET z = concat(z, x);

END WHILE;

      

RETURN z;

END $$

DELIMITER ;

 

Here, we first declare a local variable z and initialize it to an empty string (an empty string is a string with no content).

 

Next, we declare a WHILE statement. A WHILE statement can be labelled (but labelling it is optional). Here we label it while_example...

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 $15.99/month. Cancel anytime}