Switch to: V12V11V10V9V8V7V6V5

Table of Contents

Loops

Valentina SQL offers the following kinds of loops:

  • LOOP
  • WHILE
  • REPEAT … UNTIL

And you can use keywords ITERATE and LEAVE to control the loop iteration.

ITERATE

ITERATE 

ITERATE can appear only within loops (LOOP, REPEAT, WHILE) statements. ITERATE means “do the loop again“. Analog of C/C++ 'continue'.

Example:

CREATE PROCEDURE doiterate(p1 INT)
BEGIN
  LOOP
    SET p1 = p1 + 1;
    IF p1 < 10 THEN 
        ITERATE
    ELSE
        LEAVE
    END IF
  END LOOP
  SET @x = p1;
END;

LEAVE

LEAVE

LEAVE can appear only within loops (LOOP, REPEAT, WHILE) statements. Analog of C/C++ 'break'.

LOOP

LOOP
    statement_list
END LOOP

LOOP implements a simple loop construct, enabling repeated execution of the statement list, which consists of one or more statements. The statements within the loop are repeated until the loop is exited; usually this is accomplished with a LEAVE statement.

REPEAT

REPEAT
    statement_list
UNTIL search_condition
END REPEAT

The statement list within a REPEAT statement is repeated until the search_condition is true. Thus, a REPEAT always enters the loop at least once. statement_list consists of one or more statements.

Example:

CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
    DECLARE v1 INT DEFAULT 0;
 
    REPEAT 
       SET v1 = v1 + 1; 
       UNTIL v1 > p1 
    END REPEAT
END

WHILE

WHILE search_condition DO
    statement_list
END WHILE

The statement list within a WHILE statement is repeated as long as the search_condition is TRUE. statement_list consists of one or more statements.

Example:

CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;
 
  WHILE v1 > 0 DO
    ...
    SET v1 = v1 - 1;
  END WHILE
END;