PL/SQL Variable Scope & Inner Outer Block: Nested Structure
โก Smart Summary
Nested blocks in PL/SQL place one block inside another, letting you separate complex logic and isolate exception handling. Understanding variable scope between the outer and inner blocks, and using block labels, keeps same-named variables unambiguous.

What is Nested Blocks Oracle?
In PL/SQL, each block can be nested inside another block. Such a block is referred to as a nested block. Nested blocks are very common when you want to perform a certain process while keeping the code for that process in a separate container (block).
The nested block concept helps the programmer improve readability by separating complex logic into individual blocks, and it allows the exception for each block to be handled inside the main outer block.
Nested Block Structure
A block can be nested inside another block. It can be nested either in the execution part or in the exception handling part, and these blocks can also be labeled. One outer block can contain many inner blocks. Each inner block is itself a PL/SQL block, so all the properties and characteristics of the inner block are the same as those of the outer block. The parent block is the main block, and the child block is the nested block.
The image below gives a pictorial representation of the nested block structure, in which the parent block is the main block and the child block is the nested block.
Below is the syntax for the nested block. The screenshot below shows the nested block syntax with its labeled outer and inner blocks.
Nested Block Syntax
<<outer_block>> DECLARE <Declarative section> BEGIN <Execution part for outer block begins> <<inner block>> DECLARE <Declarative section> BEGIN <Execution part for inner block>. EXCEPTION <Exception handling part> END; <Execution part for outer block begins> EXCEPTION <Exception handling part> END;
- The syntax above shows the nested block that contains a total of two blocks.
- These blocks are labelled as ‘outer_block’ and ‘inner_block’.
Scopes in Nested Block: Variable Scope
In a nested block, you need to clearly understand the scope and visibility of each block before using them. Particularly in the inner block, elements from both the outer and the inner block are visible, so a proper understanding of this is necessary.
The points below summarize the scopes in nested blocks.
- The elements declared in the outer block, and the value defined before the inner block definition, are visible inside the inner block.
- The elements declared in the inner block are not visible in the outer block. They are visible only within the inner block.
- The outer block and the inner block can have a variable with the same name.
- In the case of variables with the same name, the inner block by default refers to the variable declared in the inner block only.
- If the inner block wants to refer to the outer block variable that has the same name as the inner block variable, then the outer block must be LABELLED, and the outer block variable can be referred to as ‘<outer_block_label>.<variable_name>’.
The example below helps you understand more about these scopes.
Example 1: In this example, we see the scope of variables in the inner and outer block. We also see how to refer to the variables using a block label.
The screenshot below shows Example 1, where the inner block refers to an outer block variable using the block label.
<<OUTER_BLOCK>> DECLARE var1 VARCHAR2(30):='outer_block'; var2 VARCHAR2(30):='value before inner block'; BEGIN <<INNER_BLOCK>> DECLARE var1 VARCHAR2(30):='inner_block'; BEGIN dbms_output.put_line(var1); dbms_output.put_line(OUTER_BLOCK.var1); dbms_output.put_line(var2); END; var2:='value after inner block'; END; /
Code Explanation:
- Code line 1: Labelling the outer block as “OUTER_BLOCK”.
- Code line 3: Declaring a variable ‘var1’ as VARCHAR2 (30) with the initial value of “outer_block”.
- Code line 4: Declaring a variable ‘var2’ as VARCHAR2 (30) with the initial value of “value before inner block”.
- Code line 6: Labelling the inner block as “INNER_BLOCK”.
- Code line 8: Declaring a variable ‘var1’ in the inner block as VARCHAR2 (30) with the initial value of “inner_block”.
- Code line 10: Printing the value of ‘var1’. Since no label is mentioned, by default it takes the value from the inner block, hence printing the ‘inner_block’ message.
- Code line 11: Printing the value of the outer block variable ‘var1’. Since the inner block has a variable with the same name, we refer to the outer block label. Thus it prints the ‘outer_block’ message.
- Code line 12: Printing the value of the outer block variable ‘var2’. Since there is no variable with this name in the inner block, by default it takes the value from the outer block, hence printing the ‘value before inner block’ message.
- The variable ‘var2’ in the outer block is assigned the value ‘value after inner block’. But this assignment happens after the definition of the inner block, so this value is not present inside the inner block.
Example 2: In this example, we find the difference between two numbers, one declared in the outer block and another in the inner block. Both have the same name. Let us see how the block label is useful in referring to these variables.
The screenshot below shows Example 2, which computes the difference between the outer and inner block variables of the same name.
<<OUTER_BLOCK>> DECLARE ln_val NUMBER :=5; BEGIN <<INNER_BLOCK>> DECLARE ln_val NUMBER :=3; BEGIN dbms_output.put_line('The difference between outer block and inner block variable is:'||outer_block.ln_val-inner_block.ln_val); END; END; /
Code Explanation:
- Code line 1: Labelling the outer block as “OUTER_BLOCK”.
- Code line 3: Declaring a variable ‘ln_val’ as NUMBER with the initial value of “5”.
- Code line 5: Labelling the inner block as “INNER_BLOCK”.
- Code line 7: Declaring a variable ‘ln_val’ in the inner block as NUMBER with the initial value of “3”.
- Code line 9: Printing the difference in value of ‘ln_val’ from the outer and inner block. The “<block_name>.<variable_name>” format is used to refer to these variables to avoid conflicts due to the same variable name.




