0% found this document useful (0 votes)
133 views

Nested Block in PLSQL

The document discusses nested blocks and scope of variables in PL/SQL. It provides examples showing that a variable declared in an outer block is accessible in an inner block, but not vice versa. It demonstrates using a block label to qualify references to variables in parent blocks from inner blocks when the same variable name is used. Block labels improve code readability and control of code execution.

Uploaded by

Madhava Rao N
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Nested Block in PLSQL

The document discusses nested blocks and scope of variables in PL/SQL. It provides examples showing that a variable declared in an outer block is accessible in an inner block, but not vice versa. It demonstrates using a block label to qualify references to variables in parent blocks from inner blocks when the same variable name is used. Block labels improve code readability and control of code execution.

Uploaded by

Madhava Rao N
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Few Examples

Nested Blocks:
SQL> DECLARE x NUMBER; BEGIN x := 80; DBMS_OUTPUT.PUT_LINE('abc'); DECLARE y NUMBER; BEGIN y := 90; DBMS_OUTPUT.PUT_LINE ('Inner Block variable value '|| y); END; DBMS_OUTPUT.PUT_LINE ('Outer Block variable value '|| x); END;

Output:
abc Inner Block variable value 90 Outer Block variable value 80

Scope of variables:
A variable declared in the outer block is accessible in the inner block. But a variable declared in the inner block is accessible only in the inner block.

SQL> DECLARE outer NUMBER; BEGIN outer := 80; DECLARE inner NUMBER; BEGIN inner := 90; DBMS_OUTPUT.PUT_LINE('Inner Block variable value ' || inner); DBMS_OUTPUT.PUT_LINE ('Outer block variable is accessible in the inner block'); DBMS_OUTPUT.PUT_LINE ('Outer block variable value ' || outer); END; DBMS_OUTPUT.PUT_LINE ('Outer Block variable value ' || outer); DBMS_OUTPUT.PUT_LINE ('Inner Block variable value ' || inner); END; /

Output:
DBMS_OUTPUT.PUT_LINE ('Inner Block variable value: ' || inner); * ERROR at line 14: ORA-06550: line 14, column 57: PLS-00201: identifier 'INNER' must be declared ORA-06550: line 14, column 1: PL/SQL: Statement ignored

Note: The above problem occurs, because Parent/Outer block can not access the
variable declared in the Child/Inner block. So can be avoided as follows. SQL> DECLARE outer NUMBER; BEGIN outer := 80; DECLARE inner NUMBER; BEGIN inner := 90; DBMS_OUTPUT.PUT_LINE('Inner Block variable value: ' || inner); DBMS_OUTPUT.PUT_LINE('Outer block variable is accessible in the inner block'); DBMS_OUTPUT.PUT_LINE('Outer block variable value: ' ||

outer); END; DBMS_OUTPUT.PUT_LINE ('Outer Block variable value: ' || outer); --DBMS_OUTPUT.PUT_LINE ('Inner Block variable value: ' || inner); END; /

Output:
Inner Block variable value: 90 Outer block variable is accessible in the inner block Outer block variable value: 80 Outer Block variable value: 80

PL/SQL Block Label:


So what if you want to refer to the variable in the parent block inside the child block in the above example? PL/SQL provides you a feature called block label so you can qualify all references to variables inside that block via a label. To label a block you just specify the label name before the declaration section. There are several advantages of using PL/SQL block label: Improve the readability of the code. Gain better control of code execution because a block label can be a target for EXIT and CONTINUE statements. Allow you to qualify references to variables in parent block that has the same name with variable in child block by using dot notation.

You might also like