The PL/SQL GOTO Statement
The GOTO statement
is an unconditional branch that will jump execution to the
“label” identified in the statement. It is not used very often
and some programmers claim you should never use the GOTO
statement as it will make the code harder to understand and
violates the structured programming paradigm. A label is
defined in the code with double open and close brackets.
When a GOTO statement
is encountered, program execution jumps to the code directly
after the label, so there must be at least one line of code
after the label. Likewise, the label must be in scope when the
GOTO statement is encountered. This requirement makes the GOTO
statement a lot less useful than it appears. For example the
first GOTO statements below are in scope with the labels.
2 n_numb number := &Number;
4 if n_numb < 5 then goto small_number;
5 else goto large_number;
6 end if;
8 n_numb := 25; -- goto jumps this line.
12 goto end_message;
16 goto end_message;
18 n_numb := 0; -- goto jumps this line.
value for number: 4
Enter value for number: 7
However, the example below shows that
objects declared within the IF statements are out of scope to
the outside block of code.
2 n_num number := 5;
4 goto then_clause;
5 if n_num < 8
8 n_num := 8;
9 end if;
ERROR at line 4:
ORA-06550: line 4, column 3:
PLS-00375: illegal GOTO statement
this GOTO cannot branch to label
ORA-06550: line 6, column 5:
PL/SQL: Statement ignored
Jumping out of the IF statement in the first
example is allowed because the label is in scope. Jumping into
an IF statement in the second example is not allowed because the
label is not in scope. This is also true for LOOPs, CASE
statements, and PL/SQL exceptions. Likewise, a label in a
function or procedure is not in scope to the calling block.