LEAVE statement

The LEAVE statement stops the current iteration of the containing WHILE, REPEAT, LOOP, or BEGIN statement identified by Label.

The containing statement's evaluation of its loop condition (if any) is bypassed and looping stops.

Syntax

Read syntax diagramSkip visual syntax diagramLEAVELabel

Examples

In the following example, the loop iterates four times:
DECLARE i INTEGER;
SET i = 1;
X : REPEAT 
  ...
  IF i>= 4 THEN
    LEAVE X;
  END IF;

  SET i = i + 1;
UNTIL
  FALSE
END REPEAT;
LEAVE statements do not have to be directly contained by their labelled statement, making LEAVE statements particularly powerful.
DECLARE i INTEGER;
SET i = 0;
X : REPEAT                   -- Outer loop
  ...
  DECLARE j INTEGER;
  SET j = 0;
  REPEAT                     -- Inner loop
    ...
    IF i>= 2 AND j = 1 THEN
      LEAVE X;               -- Outer loop left from within inner loop
    END IF;
    ...
    SET j = j + 1;
  UNTIL
    j>= 3
  END REPEAT;

  SET i = i + 1;
UNTIL
  i>= 3
END REPEAT X;
                             -- Execution resumes here after the leave