Oracle PL/SQL - Label and Subprogram with Same Name in Same Scope

Introduction

In the same scope, you should give labels and subprograms unique names to avoid confusion and unexpected results.

In the following code, my_code is the name of both a block and a subprogram.

Both the block and the subprogram declare a variable named x.

In the subprogram, my_code.x refers to the local variable x, not to the global variable x.

Demo

SQL>
SQL> <<my_code>>--  w w w  .j av a 2s. c o m
  2  DECLARE
  3    x  NUMBER := 5;
  4
  5    PROCEDURE my_code AS
  6      x  NUMBER := 0;
  7    BEGIN
  8      DBMS_OUTPUT.PUT_LINE('x = ' || x);
  9      DBMS_OUTPUT.PUT_LINE('my_code.x = ' || my_code.x);
 10    END;
 11
 12  BEGIN
 13    my_code;
 14  END;
 15  /
x = 0
my_code.x = 0

PL/SQL procedure successfully completed.

SQL>

Related Topic