Skip to content

BREAK

BREAK terminates a loop within a scripting block (see also BEGIN ... END).

See also CONTINUE.

Syntax

sql
...
BREAK [label];
...

or

...
EXIT [label];
...

Arguments:

label (optional): If the label is specified, BREAK causes the scripting block to execute from the statement immediately following the label.

This is useful when a scripting block consists of more than one level, a nested loop, or a nested branching.

Usage Notes

With BREAK, it is possible to exit not only the current loop but also an enclosing loop. To do this, include the label of the enclosing loop as part of the BREAK statement.

Example 1 | BREAK

The following is an example of a nested while loop. In the inner loop, both loops are terminated when the IF condition is met.

sql
DECLARE
  ix_1 INTEGER;
  ix_2 INTEGER;
BEGIN
  ix_1 := 1;
  ix_2 := 1;
  WHILE (ix_1 <= 4) DO
    WHILE (ix_2 <= 4) DO
      -- BREAK when ix_2 = 3 (regardless of ix_1).
      IF (ix_2 = 3) THEN
        BREAK extern_loop; -- Jump to 'END WHILE extern_loop'
      END IF;
      ix_2 := ix_2 + 1;
    END WHILE intern_loop;
    ix_1 := ix_1 + 1;
  END WHILE extern_loop;
  -- After the BREAK, the block continues execution from here.
  select :ix_2 as ix_2, :ix_1 as ix_1;
END;
+------+------+ 
| ix_2 | ix_1 | 
+------+------+
| 3    | 1    | 
+------+------+

Example 2 | EXIT

AlexE: Im Moment hängt dieses Beispiel MKW auf...

Löschen ?

The following is a simple example of the EXIT command, which terminates the while loop.

sql
DECLARE
  ix_1 INTEGER;
BEGIN
  ix_1 := 1;
  WHILE (TRUE) DO
    ix_1 := ix_1 + 1;
    IF (ix_1 = 3) THEN
      EXIT;
    END IF;
  END WHILE;
  select :ix_1 as ix_1;
END;
+------+ 
| ix_1 | 
+------+
| 3    | 
+------+