Chapter 8 Iterative Control

1)Rewrite script ch08_1a.sql using a WHILE loop instead of a simple loop. Make sure that the output produced by this script does not differ from the output produced by the script ch08_1a.sql.
A1: Answer: Consider the script ch08_1a.sql:
SET SERVEROUTPUT ON 
DECLARE 
   v_counter BINARY_INTEGER := 0; 
BEGIN 
   LOOP 
      -- increment loop counter by one 
      v_counter := v_counter + 1; 
      DBMS_OUTPUT.PUT_LINE ('v_counter = '||v_counter); 

      -- if EXIT condition yields TRUE exit the loop 
      IF v_counter = 5 THEN 
         EXIT; 
      END IF; 

   END LOOP; 
   -- control resumes here 
   DBMS_OUTPUT.PUT_LINE ('Done…'); 
END; 

Next, consider a new version of the script that uses a WHILE loop. All changes are shown in bold letters.

 SET SERVEROUTPUT ON DECLARE v_counter BINARY_INTEGER ...

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.