Pass The Control To Specific Line Using Goto Label Command In PLSQL

An example is given below for PLSQL Goto Label command to instruct the control to go to specific line number on some condition.
Suppose you want to by pass the control to any particular line on exception when no_data_found occurs, to achieve this you have to give a label using <<anylabel>> to that particular line to point it at the time of requirement.
The following is an anonymous PL/SQL block demonstrating this scenario using <<Labels>> with GoTo Label statement:
SET SERVEROUTPUT ON;
DECLARE
   v   NUMBER;
BEGIN
   BEGIN
      SELECT 1
        INTO v
        FROM DUAL
       WHERE dummy = 'V';
   -- change dummy = 'X' to skip exception section
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         DBMS_OUTPUT.put_line ('NO_Data_Found occured.');
         GOTO mylabel;
   END;
   -- below code before <<mylabel>> would not be executed if an error occured above
   -- because in exception section above, it is sending control directly to label <<mylabel>>
   -- but if no error occurred above then the whole program would be executed
   v := v + 1;
   DBMS_OUTPUT.put_line ('The value of variable v is '||v);
  <<mylabel>>
   DBMS_OUTPUT.put_line ('After label processing starts.');
-- do some task here
END;
/
Labels PLSQL

Leave a Comment