Firebird Documentation IndexFirebird 2.5 Language Ref. UpdatePSQL statements → LEAVE
Firebird Home Firebird Home Prev: IN AUTONOMOUS TRANSACTIONFirebird Documentation IndexUp: PSQL statementsNext: OPEN cursor

LEAVE

Available in: PSQL

Added in: 1.5

Changed in: 2.0

Description: LEAVE immediately terminates the innermost WHILE or FOR loop. With the optional label argument introduced in Firebird 2.0, LEAVE can break out of surrounding loops as well. Execution continues with the first statement after the outermost terminated loop.

Syntax: 

[label:]
{FOR | WHILE} ... DO
   ...
   (possibly nested loops, with or without labels)
   ...
   LEAVE [label];

Example: 

If an error occurs during the insert in the example below, the event is logged and the loop terminated. The program continues at the line of code reading c = 0;

while (b < 10) do
begin
  insert into Numbers(B) values (:b);
  b = b + 1;
  when any do
  begin
    execute procedure log_error (current_timestamp, 'Error in B loop');
    leave;
  end
end
c = 0;

The next example uses labels. Leave LoopA terminates the outer loop, leave LoopB the inner loop. Notice that a plain leave would also suffice to terminate the inner loop.

stmt1 = 'select Name from Farms';
LoopA:
for execute statement :stmt1 into :farm do
begin
  stmt2 = 'select Name from Animals where Farm = ''';
  LoopB:
  for execute statement :stmt2 || :farm || '''' into :animal do
  begin
    if  (animal = 'Fluffy') then leave LoopB;
    else if (animal = farm) then leave LoopA;
    else suspend;
  end
end
Prev: IN AUTONOMOUS TRANSACTIONFirebird Documentation IndexUp: PSQL statementsNext: OPEN cursor
Firebird Documentation IndexFirebird 2.5 Language Ref. UpdatePSQL statements → LEAVE