Quick Search:
 
 The Oracle PL/SQL GOTO Statement      [Return To Index] Jump to:  

Term: GOTO

Definition:
The Oracle PL/SQL GOTO statement is a sequential control structure available in Oracle. The GOTO statement immediately transfers program control (called "branching") unconditionally to a named statement label or block label. The statement or label name must be unique in the block.

Notes:

  1. A GOTO statement cannot branch into an IF statement, a LOOP statement, or a sub-block.
  2. A GOTO label must lead to an executable statement or a PL/SQL block.
  3. If GOTO is used to exit a cursor FOR loop prematurely, the cursor is automatically closed.
  4. A GOTO can branch to another location in the current block or into an enclosing block, but it cannot branch into an exception handler.

Stylistic Considerations:
The use of GOTO is often considered to be poor programming style. Overuse of GOTO can lead to "spaghetti code" or code which is convoluted and difficult to follow. In general, avoid using GOTO statements unless absolutely necessary.

Example Syntax:

GOTO [LABEL or BLOCK NAME]

Example Usage:

BEGIN
...
GOTO insert_row;
...
<<insert_row>>
INSERT INTO EMPLOYEES VALUES ...
END;



To use GOTO to jump a place that does not have an executable statement, add the NULL statement:

DECLARE
done BOOLEAN;
BEGIN
FOR i IN 1..1000 LOOP
IF done THEN
GOTO end_loop;
END IF;
<<end_loop>> -- this is not allowed unless an executable statement follows
NULL; -- add a NULL statement to avoid raising an error
END LOOP; -- this will raise an error unless the previous NULL is present
END;

 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org