SERPland Blog

pl/sql: exit an enclosing loop (leaving a labeled loop)

· 478 words · 3 minutes to read

You can leave a loop in pl/sql by specifying a nice litte label to the loop section. For example: «firstloop»

When labeling, you can exit the loop at any time. In following example, lets give the first loop the label «firstloop» and so on… The third loop contains an exit statement when reaching the value of 2. With this exit the control goes back to the end of the first loop!

Usually an exit without labels would only exit the third loop. The first and second loop would run forward.

With this pl/sql label we can skip all of them!

Here’s the sample pl/sql code: begin dbms_output.put_line('start first loop'); <<firstloop>> for a in 1..3 loop dbms_output.put_line('a=' ||a); <<secondloop>> for b in 1..3 loop dbms_output.put_line(' b=' ||b); <<thirdloop>> for c in 1..3 loop dbms_output.put_line(' c=' ||c); exit firstloop when c=2; end loop thirdloop; end loop secondloop;

end loop firstloop; end;

Result: start first loop a=1 b=1 c=1 c=2


Update 2024

Update on PL/SQL Exit Statement with Labeled Loops in 2024 🔗

Der PL/SQL-Exit-Befehl, der es ermöglicht, eine Schleife zu verlassen, indem ein Label angegeben wird, das diese Schleife umgibt, ist auch im Jahr 2024 noch gültig. Durch die Verwendung von Labels können Entwickler:innen die Kontrolle über den Fluss ihres Codes behalten und spezifischere Ausstiegsbedingungen festlegen.

Im Jahr 2024 können Entwickler:innen nach wie vor ein Label für eine Schleife setzen, um sie bei Bedarf zu verlassen. Zum Beispiel könnte ein Label “firstloop” verwendet werden, um die erste Schleife im Code zu kennzeichnen. Durch das Label kann die Schleife an beliebiger Stelle verlassen werden, was besonders nützlich ist, wenn komplexe Logik oder Sonderfälle berücksichtigt werden müssen.

Ein Beispiel für die Verwendung eines Exit-Statements mit einem Label in PL/SQL könnte wie folgt aussehen:

begin
  dbms_output.put_line('Starte erste Schleife - firstloop');
  <<firstloop>>
  for a in loop
    dbms_output.put_line(a);
    <<secondloop>>
    for b in loop
      dbms_output.put_line(b);
      <<thirdloop>>
      for c in loop
        dbms_output.put_line(c);
        exit firstloop when c = 3;
      end loop thirdloop;
    end loop secondloop;
  end loop firstloop;
end;

In diesem Beispiel zeigt sich die Flexibilität des Exit-Statements mit Labels. Wenn der Wert von c in der dritten Schleife 3 erreicht, wird die Ausführung zur Endung der ersten Schleife zurückgeführt. Ohne das Label wäre ein exit-Statement ohne spezifiziertes Label nur die dritte Schleife verlassen.

Im Jahr 2024 bleibt die Verwendung von Labels in PL/SQL eine bewährte Methode, um die Struktur und Steuerung von Schleifen in komplexen Codeabschnitten zu verbessern. Entwickler:innen können weiterhin von dieser Funktion profitieren, um ihren Code übersichtlicher und effizienter zu gestalten.


In this updated scenario in 2024, the PL/SQL exit statement with labeled loops remains a valid and useful feature for developers working with Oracle databases. The ability to control the flow of loops using labels provides a clear and efficient way to manage complex logic in code. By utilizing labeled loops, developers can maintain a high level of control and readability in their PL/SQL code.