Sub topic 9
Nested Blocks, Scope, and Shadowing
This chapter assumes that you can run anonymous PL/SQL blocks and understand variables, assignments, and the DECLARE, BEGIN, and END sections. You should also know that DBMS_OUTPUT.PUT_LINE displays text when server output is enabled.
A PL/SQL block can contain another block. The block inside is a nested block. Nested blocks let you limit a variable's lifetime and keep temporary logic close to where it is used.
How scope works
Scope is the part of a program where a declared name can be used. A variable declared in an outer block is visible in its nested blocks. A variable declared in a nested block is not visible in the outer block.
The declaration section creates variables. The executable section can use variables that are in scope. When execution leaves a block, variables declared in that block cease to exist.
| Declaration location | Visible in the declaring block | Visible in nested blocks | Visible after the block ends |
|---|---|---|---|
| Outer block | Yes | Yes | No |
| Inner block | Yes | Yes, to deeper nested blocks | No |
Reading outer variables
An inner block can read and change a variable from its outer block. The change remains visible to the outer block after the inner block finishes.
SET SERVEROUTPUT ON
DECLARE
v_message VARCHAR2(30) := 'Before inner block';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_message);
DECLARE
BEGIN
v_message := 'Changed by inner block';
DBMS_OUTPUT.PUT_LINE(v_message);
END;
DBMS_OUTPUT.PUT_LINE(v_message);
END;
/The inner block does not declare v_message, so PL/SQL looks outward and finds the variable in the outer block. The output is:
Before inner block
Changed by inner block
Changed by inner blockA nested block can also declare its own variables. Those variables are useful for intermediate calculations that should not be available elsewhere.
Shadowing a variable
Shadowing occurs when an inner block declares a name that already exists in an outer block. Inside the inner block, the inner declaration takes precedence. The outer variable still exists, but its name is hidden.
SET SERVEROUTPUT ON
DECLARE
v_level VARCHAR2(10) := 'outer';
BEGIN
DBMS_OUTPUT.PUT_LINE('Outer before: ' || v_level);
DECLARE
v_level VARCHAR2(10) := 'inner';
BEGIN
DBMS_OUTPUT.PUT_LINE('Inside: ' || v_level);
END;
DBMS_OUTPUT.PUT_LINE('Outer after: ' || v_level);
END;
/The output is:
Outer before: outer
Inside: inner
Outer after: outerThe inner assignment or declaration does not change the outer variable. It refers to a different variable with the same name.
Qualifying a shadowed name
You can give a block a label and use that label to refer to a variable declared by the labeled block. This makes the intended variable explicit.
SET SERVEROUTPUT ON
<<outer_block>>
DECLARE
v_level VARCHAR2(10) := 'outer';
BEGIN
DECLARE
v_level VARCHAR2(10) := 'inner';
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner: ' || v_level);
DBMS_OUTPUT.PUT_LINE('Outer: ' || outer_block.v_level);
END;
END;
/The expression outer_block.v_level selects the variable from the labeled outer block. Use qualification when a nested block must work with both variables.
Practical rules
- Declare a variable in the narrowest block that needs it.
- Let nested blocks read outer variables when that improves clarity.
- Prefer distinct names when nested logic uses related values.
- Use block labels to document an intentional shadowing case.
- Remember that a nested block's variables are unavailable after that block ends.
Exercise
In the example below, which value does the final line print, and why? DECLARE vcount NUMBER := 1; BEGIN DECLARE vcount NUMBER := 2; BEGIN NULL; END; DBMSOUTPUT.PUTLINE(v_count); END; /
Show answer
It prints 1. The inner declaration shadows the outer variable only inside the nested block. After that block ends, the outer v_count is visible again.