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.
- A GOTO statement cannot branch into an IF statement, a LOOP statement, or a sub-block.
- A GOTO label must lead to an executable statement or a PL/SQL block.
- If GOTO is used to exit a cursor FOR loop prematurely, the cursor is automatically closed.
- A GOTO can branch to another location in the current block or into an enclosing block, but it cannot branch into an exception handler.
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.
GOTO [LABEL or BLOCK NAME]
INSERT INTO EMPLOYEES VALUES ...
To use GOTO to jump a place that does not have an executable statement, add the NULL statement:
FOR i IN 1..1000 LOOP
IF done THEN
<<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