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.
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:
From a block or to an executable statement
From an exception handler into an enclosing block
Transfer of control with the GOTO statement is not allowed in the following places:
From one IF statement clause to another
From an enclosing block into a subblock
From an exception handler into the current block
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.