3-13. Exiting an Outer Loop Prematurely

Problem

Your code contains a nested loop, and you want the inner loop to have the ability to exit from both loops and stop iteration completely.

Solution

Use loop labels for both loops and then reference either loop within an EXIT statement by following the EXIT keyword with a loop label. The following example prints out a series of numbers. During each iteration, the inner loop will increment until it reaches an odd number. At that point, it will pass control to the outer loop again. The outer loop will be exited when the index for the inner loop is greater than or equal to the number ten.

BEGIN   <<outer>> for idx1 in 1 .. 10 loop     <<inner>> for idx2 in 1 .. 10 loop       dbms_output.put(idx2);       exit ...

Get Oracle and PL/SQL Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.