Oracle PL/SQL Tutorial - PL/SQL GOTO






The GOTO statement performs unconditional branching to another executable statement in the same execution.

The general format for a GOTO statement is:


GOTO label_name; 

where label_name is the name of a label identifying the target statement.

This GOTO label is defined in the program as follows:

 
<<label_name>>

Example

When executed, the GOTO statement transfers control to the labeled statement or block.


DECLARE -- from   www  .j a va  2 s .c o m
  p  VARCHAR2(30); 
  n  PLS_INTEGER := 100; 
BEGIN 
  FOR j in 2..ROUND(SQRT(n)) LOOP 
        IF n MOD j = 0 THEN 
              p := ' is not a prime number'; 
              GOTO my_label; 
        END IF; 
  END LOOP; 
  p := ' is a prime number'; 
  <<my_label>> 
  DBMS_OUTPUT.PUT_LINE(TO_CHAR(n) || p); 
END; 
/ 

The code above generates the following result.





Example 2

The following code shows how to use a NULL Statement to Allow a GOTO to a Label.


DECLARE --  w w w.j  a v  a  2  s.  c o  m
  done  BOOLEAN; 
BEGIN 
  FOR i IN 1..50 LOOP 
    IF done THEN 
      GOTO end_loop; 
    END IF; 
    <<end_loop>> 
    NULL; 
  END LOOP; 
END; 
/