?
Scope |
Top Previous Next |
Scope of elements
The declared element is local to its own block. DECLARE -- outer v_employee_id NUMBER(3) := 100; v_last_name VARCHAR2(30) := Clinton; BEGIN DECLARE -- inner v_last_name VARCHAR2(30) := Obama; BEGIN DBMS_OUTPUT.PUT_LINE(ID = || v_employee_id); DBMS_OUTPUT.PUT_LINE(Name (inner) = || v_last_name); END; -- inner DBMS_OUTPUT.PUT_LINE(Name (outer) = || v_last_name); END; -- outer
The output ID = 100 Name (inner) = Obama Name (outer) = Clinton
To make it more clear one can use block labels: <<outer_block>> DECLARE v_last_name VARCHAR2(30) := Clinton; BEGIN <<inner_block>> DECLARE v_last_name VARCHAR2(30) := Obama; BEGIN DBMS_OUTPUT.PUT_LINE(outer_block.v_last_name); END; END; Here PUT_LINE uses the label prefix to directly reference the outer block variable, bypassing the inner block variable. As a result Clinton will be displayed.
I personally do not think it is a good idea to declare separate variables in each block. I would stick with good old Progress rule and declare all variables in the header. And do the same to exception handling. I put it here to show that feature of Oracle for your information.
|