Control Structures  «Prev  Next»

Lesson 6 Labels and the GOTO statement
Objective Define uses for labels and the GOTO statement.

Labels and the GOTO statement in Oracle

In this lesson, you will learn about labels[1] and the GOTO statement.
If an object is declared in an enclosing block and redeclared in a subblock, the subblock cannot reference the object in the enclosing block, unless the enclosing block is labeled by using label_name.

<<my_label>>
BEGIN
  statement_1,..., statement_n
END;
PL-SQL labels consisting of 1) an undeclared identifier 2) A sequence of statements

If label_name is used at the beginning of the block definition to improve the readability of the code, you should also use it at the end of the definition.

GOTO statement

The GOTO statement branches to a label unconditionally. When executed, the GOTO statement changes the flow of control in PL/SQL block. The two parts needed to code a GOTO statement are label_name_declaration and goto_statement.
Transfer of control with the GOTO statement is allowed in the following places:
  1. From a block or to an executable statement
  2. From an exception handler into an enclosing block

Transfer of control with the GOTO statement is not allowed in the following places:
  1. From one IF statement clause to another
  2. From an enclosing block into a subblock
  3. From an exception handler into the current block
  4. Out of a subprogram

Block Goto Statement -Exercise

The next lesson concludes this module.
Click the Exercise link below to create a PL/SQL block that uses labels and a GOTO statement.
Block Goto Statement -Exercise

[1]Labels: A label name is optionally used to name the PL/SQL block.