Variable scope and Nested Blocks.

0
Variable scope and Nested Blocks:
Scope:

  • The scope of an identifier is that region of a program unit (block, subprogram, or package) from which you can reference the identifier.
  • Once the scope of the variable is lost it means that the life of the variable no more.
  • All variables loose their scope as soon as the PL/SQL block ends or terminates.
  • Oracle automatically release the space upon that variable once its scope is closed.
Concepts of scoping:
  • With in the same scope, all identifiers must be unique.
  • Even when the data types are differing, variables and parameters cannot share the same name.
Nested blocks:
  • PL/SQL blocks can be nested wherever an executable statement is allowed.
  • A nested block becomes a statement.
  • An exception section can contain nested blocks.

Scope  and visibility diagram:
Ex:   
SQL>DECLARE
            x NUMBER;
         BEGIN
              DECLARE
                 y NUMBER;
              BEGIN
                 y:=x;
              END;
            …
         END;
SQL>DECLARE
               
              m NUMBER:=250;
         BEGIN
               
              DBMS_OUTPUT.PUT_LINE(‘The value of Outer m:’||m);
              DECLARE
                   
                     m NUMBER:=550;
              BEGIN
                  
                     DBMS_OUTPUT.PUT_LINE(‘The value of Outer m:’||m);
             END;
               
         DBMS_OUTPUT.PUT_LINE(‘The value of Outer m:’||m);          
       END;

SQL>DECLARE
               
               m NUMBER:=100;
         BEGIN

              m:=200;
              DBMS_OUTPUT.PUT_LINE(‘The value of Outer m:’||m);
              DECLARE

                   n NUMBER:=300;
                   v_tol NUMBER;
              BEGIN

                   m:=500;
                   n:=600;
                   v_tol:=m+n;
                  DBMS_OUTPUT.PUT_LINE(‘The sum of m,n is:’||v_tol);
             END;

           DBMS_OUTPUT.PUT_LINE(‘The value of Outer m:’||m) 
            END;

 Click here to go to Back page

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts