Index | Home | Forums
Oracle PL/SQL Example 2-19 PL/SQL Block Using Multiple and Duplicate Labels

Example 2-19 PL/SQL Block Using Multiple and Duplicate Labels

<<compute_ratio>>
<<another_label>>
DECLARE
   numerator   NUMBER := 22;
   denominator NUMBER := 7;
   the_ratio   NUMBER;
  BEGIN 
     <<inner_label>>
     <<another_label>>
   DECLARE
      denominator NUMBER := 0;
    BEGIN
      -- first use the denominator value = 7 from global DECLARE 
      -- to compute a rough value of pi
      the_ratio := numerator/compute_ratio.denominator;
      DBMS_OUTPUT.PUT_LINE('Ratio = ' || the_ratio);
      -- now use the local denominator value = 0 to raise an exception
      -- inner_label is not needed but used for clarification
      the_ratio := numerator/inner_label.denominator;
      DBMS_OUTPUT.PUT_LINE('Ratio = ' || the_ratio);
      -- if you use a duplicate label, you might get errors 
      -- or unpredictable results
      the_ratio := numerator/another_label.denominator;
      DBMS_OUTPUT.PUT_LINE('Ratio = ' || the_ratio);
    EXCEPTION
      WHEN ZERO_DIVIDE THEN
        DBMS_OUTPUT.PUT_LINE('Divide-by-zero error: can''t divide ' 
         || numerator || ' by ' || denominator);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Unexpected error.');
  END inner_label;
END compute_ratio;
/

This page was created by oracleabc.com on Feb 23,2008 00:09:02
General comments and questions regarding this document should be sent by email to info@oracleabc.com or post your comments here