GOTO Statement

In PL/SQL, GOTO statement makes you able to get an unconditional jump from the GOTO to a specific executable statement label in the same subprogram of the PL/SQL block.

Here the label declaration which contains the label_name encapsulated within the << >> symbol and must be followed by at least one statement to execute.

Syntax:

GOTO label_name;  

Here the label declaration which contains the label_name encapsulated within the << >> symbol and must be followed by at least one statement to execute.

GOTO label_name;  

 ..  

..  

<<label_name>>  

Statement;

Example of PL/SQL GOTO statement

Let’s take an example of PL/SQL GOTO statement.

DECLARE  

   a number(2) := 30;  

BEGIN  

   <<loopstart>>  

   -- while loop execution   

   WHILE a < 50 LOOP  

      dbms_output.put_line ('value of a: ' || a);  

      a := a + 1;  

      IF a = 35 THEN  

         a := a + 1;  

         GOTO loopstart;  

      END IF;  

   END LOOP;  

END;  

/ 

    After the execution of above code, you will get the following result:

    value of a: 30
    value of a: 31
    value of a: 32
    value of a: 33
    value of a: 34
    value of a: 36
    value of a: 37
    value of a: 38
    value of a: 39
    value of a: 40
    value of a: 41
    value of a: 42
    value of a: 43
    value of a: 44
    value of a: 45
    value of a: 46
    value of a: 47
    value of a: 48
    value of a: 49
    
    Statement processed.
    

    Restriction on GOTO statement

    Following is a list of some restrictions imposed on GOTO statement.

    • Cannot transfer control into an IF statement, CASE statement, LOOP statement or sub-block.
    • Cannot transfer control from one IF statement clause to another or from one CASE statement WHEN clause to another.
    • Cannot transfer control from an outer block into a sub-block.
    • Cannot transfer control out of a subprogram.
    • Cannot transfer control into an exception handler.

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *