Lab 8.4 Nested Loops

Lab Objectives

After this Lab, you will be able to:

Use Nested Loops

You have explored three types of loops: simple loops, WHILE loops, and numeric FOR loops. Any of these three types of loops can be nested inside one another. For example, a simple loop can be nested inside a WHILE loop and vice versa. Consider the following example:

FOR EXAMPLE

DECLARE 
   v_counter1 INTEGER := 0; 
   v_counter2 INTEGER; 
BEGIN 
   WHILE v_counter1 < 3 LOOP 
      DBMS_OUTPUT.PUT_LINE ('v_counter1: '||v_counter1); 
      v_counter2 := 0; 
      LOOP
					DBMS_OUTPUT.PUT_LINE ('v_counter2: '||v_counter2);
					v_counter2 := v_counter2 + 1;
					EXIT WHEN v_counter2 >= 2;
					END LOOP; 
      v_counter1 := v_counter1 + 1; 
   END LOOP; 
END; 

In this example, the WHILE loop is called an outer loop ...

Get Oracle® PL/SQL® Interactive Workbook, Second Edition 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.