Mariadb Repeat Statement

MariaDB: REPEAT Statement

This MariaDB tutorial explains how to use the REPEAT statement (REPEAT UNTIL LOOP) in MariaDB with syntax and examples.

Description

In MariaDB, the REPEAT statement is used when you do not know how many times you want the loop body to execute.

Syntax

The syntax for the REPEAT statement in MariaDB is:

[ <strong>label_name</strong>: ] REPEAT
   {...statements...}
UNTIL <strong>condition</strong>
END REPEAT [ <strong>label_name</strong> ];

Parameters or Arguments

label_name

Optional. The name associated with the REPEAT loop.

statements

The statements of code to execute each pass through the REPEAT loop.

condition

The condition that will terminate the REPEAT loop.

Note

  • You would use a REPEAT statement when you are unsure of how many times you want the loop body to execute.
  • You terminate a REPEAT statement with the UNTIL condition.

Example

Let's look at an example that shows how to use the REPEAT statement in MariaDB:

DELIMITER //

CREATE FUNCTION CalcValue ( starting_value INT )
RETURNS INT DETERMINISTIC

BEGIN

   DECLARE total_value INT;

   SET total_value = 0;

   label1: REPEAT
     SET total_value = total_value + starting_value;
   UNTIL total_value >= 999 
   END REPEAT label1;

   RETURN total_value;

END; //

DELIMITER ;

In this MariaDB LOOP example, the REPEAT statement would repeat the loop until total_value is greater than or equal to 999, at which point the REPEAT loop would be terminated.