O'Reilly logo

Oracle® PL/SQL® Interactive Workbook, Second Edition by Elena Silvestrova, Benjamin Rosenzweig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required