Sample Text

Scope of PL/Sql variables

Scope of PL/SQL Variables

PL/SQL allows the nesting of Blocks within Blocks i.e., the Execution section of an outer block can contain inner blocks. Therefore, a variable which is accessible to an outer Block is also accessible to all nested inner Blocks. The variables declared in the inner blocks are not accessible to outer blocks. Based on their declaration we can classify variables into two types.
  • Local variables - These are declared in an inner block and cannot be referenced by outside Blocks.
  • Global variables - These are declared in an outer block and can be referenced by its itself and by its inner blocks.
For Example: In the below example we are creating two variables in the outer block and assigning their product to the third variable created in the inner block. The variable 'var_mult' is declared in the inner block, so cannot be accessed in the outer block i.e. it cannot be accessed after line 11. The variables 'var_num1' and 'var_num2' can be accessed anywhere in the block.


Example

/* Formatted on 2017/01/24 19:38 (Formatter Plus v4.8.8) */
DECLARE
   var_num1   NUMBER;
   var_num2   NUMBER;
BEGIN
   var_num1 := 100;
   var_num2 := 200;

   DECLARE
      var_mult   NUMBER;
   BEGIN
      var_mult := var_num1 * var_num2;
   END;
END;
/

Output Will be 20000 Because it will calculate inner value first as per Plsql Scope.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *