<<label_name>>
goto_statement ::=
GOTO label_name;
BEGIN ... GOTO update_row; -- illegal branch into IF statement ... IF valid THEN ... <<update_row>> UPDATE emp SET ... END IF; END;
From the current block, a GOTO statement can branch to another place in the block or into an enclosing block, but not into an exception handler. From an exception handler, a GOTO statement can branch into an enclosing block, but not into the current block.
If you use the GOTO statement to exit a cursor FOR loop prematurely, the cursor is closed automatically. The cursor is also closed automatically if an exception is raised inside the loop.
A given label can appear only once in a block. However, the label can appear in other blocks including enclosing blocks and sub-blocks. If a GOTO statement cannot find its target label in the current block, it branches to the first enclosing block in which the label appears.
BEGIN ... FOR ctr IN 1..50 LOOP DELETE FROM emp WHERE ... IF SQL%FOUND THEN GOTO end_loop; -- illegal END IF; ... <<end_loop>> END LOOP; -- not an executable statement END;
To debug the last example, simply add the NULL statement, as follows:
BEGIN ... FOR ctr IN 1..50 LOOP DELETE FROM emp WHERE ... IF SQL%FOUND THEN GOTO end_loop; END IF; ... <<end_loop>> NULL; -- an executable statement that specifies inaction END LOOP; END;
For more examples of legal and illegal GOTO statements, see "GOTO Statement" .