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.

PL-SQL labels Labels
  statement_1,..., statement_n
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.
Oracle PL/SQL Programming

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

Does the Oracle PL/SQL Language still support the GOTO Statement?

As of January 2023, the Oracle PL/SQL language still supports the GOTO statement. It can be used to transfer control to a labeled statement within the same PL/SQL block or subprogram. However, it is generally considered bad programming practice to use the GOTO statement, as it can make code difficult to read and understand, and it can lead to unexpected behavior. Instead, it is recommended to use more structured control flow constructs such as loops and conditional statements.

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.