Illustrates the scope of various identifiers : Variable Scope « PL SQL « Oracle PL / SQL






Illustrates the scope of various identifiers

 

SQL>
SQL> --Illustrates the scope of various identifiers.
SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2     a_name  VARCHAR2(30) := 'A Name';
  3
  4  PROCEDURE name_print IS
  5  BEGIN
  6     DBMS_OUTPUT.PUT_LINE(a_name);
  7  END;
  8
  9
 10  BEGIN
 11     DBMS_OUTPUT.PUT_LINE(a_name);
 12
 13     DECLARE
 14         b_name  VARCHAR2(30) := 'a Name';
 15     BEGIN
 16         DBMS_OUTPUT.PUT_LINE('Inside nested block');
 17        DBMS_OUTPUT.PUT_LINE(a_name);
 18        DBMS_OUTPUT.PUT_LINE(b_name);
 19        name_print;
 20     END;
 21
 22         DBMS_OUTPUT.PUT_LINE('Back in the main block');
 23         --But we cannot compile the following line because b_name
 24        --is not defined in this block.
 25        --DBMS_OUTPUT.PUT_LINE(b_name);
 26        name_print;
 27  END;
 28
 29
 30  /
A Name
Inside nested block
A Name
a Name
A Name
Back in the main block
A Name

PL/SQL procedure successfully completed.

SQL>

           
         
  








Related examples in the same category

1.Variable scope in variable 'declare' block
2.Variable scope in a PL/SQL
3.Identifiers defined in the declaration of the child block are only in scope and visible within the child block itself.
4.if you try to reference a variable before you declare it because PL/SQL requires an identifier be declared before we use it in our code
5.This type is local to this block
6.Reference Type in another block as well
7.variable scope with nested block
8.Override your scope access to containing blocks by reusing an identifier in a nested block