More examples:
In this example, I am declaring v_first in the outer and v_second in the inner. In the
inner I can use v_first because it was declared in the outer. Note that both v_first and
v_second are displayed.
SET SERVEROUTPUT ON
DECLARE
v_first NUMBER :=212;
v_msg VARCHAR2(20) :='* Outer define *';
out_msg VARCHAR2(20);
out_ans NUMBER;
BEGIN
out_msg := v_msg;
out_ans := v_first *2;
dbms_output.put_line('BEFORE INNER DEFINE');
dbms_output.put_line(v_first||v_msg||out_msg||out_ans);
DECLARE
v_second NUMBER := 555;
v_msg VARCHAR2(20) := '* Inner define *';
in_msg VARCHAR2(20);
in_ans NUMBER;
BEGIN
in_msg := v_msg;
in_ans := v_first * 2;
dbms_output.put_line('INNER LOOP SHOWING INNER');
dbms_output.put_line(v_first||v_second||v_msg||in_msg||in_ans);
dbms_output.put_line('INNER LOOP SHOWING OUTER');
dbms_output.put_line(out_msg||out_ans);
END;
dbms_output.put_line('AFTER INNER/BACK TO OUTER'||v_first||v_msg);
dbms_output.put_line('OUTER LOOP SHOWING OUTER'||out_msg||out_ans);
/* Note that when I try to show inner definitions here, in_msg and
in_ans it crashes */
END;
/
SET SERVEROUTPUT OFF
SQL> @ nest2_intro
BEFORE INNER DEFINE
212* Outer define ** Outer define *424
INNER LOOP SHOWING INNER
212555* Inner define ** Inner define *424
INNER LOOP SHOWING OUTER
* Outer define *424
AFTER INNER/BACK TO OUTER212* Outer define *
OUTER LOOP SHOWING OUTER* Outer define *424
PL/SQL procedure successfully completed.
In this example, I am trying to use v_second which was declared in the inner after I have
closed in inner and returned to the outer. Note that the program does not work because it
claims that v_second has not been declared. The outer is not aware of the declarations in
the inner.
SET SERVEROUTPUT ON
DECLARE
v_first NUMBER :=212;
v_msg VARCHAR2(20) :='* Outer define *';
out_msg VARCHAR2(20);
out_ans NUMBER;
BEGIN
out_msg := v_msg;
out_ans := v_first *2;
dbms_output.put_line('BEFORE INNER DEFINE');
dbms_output.put_line(v_first||v_msg||out_msg||out_ans);
DECLARE
v_second NUMBER := 555;
v_msg VARCHAR2(20) := '* Inner define *';
in_msg VARCHAR2(20);
in_ans NUMBER;
BEGIN
in_msg := v_msg;
in_ans := v_first * 2;
dbms_output.put_line('INNER LOOP SHOWING INNER');
dbms_output.put_line(v_first||v_second||v_msg||in_msg||in_ans);
dbms_output.put_line('INNER LOOP SHOWING OUTER');
dbms_output.put_line(out_msg||out_ans);
END;
dbms_output.put_line('AFTER INNER/BACK TO OUTER'||v_first||v_second||v_msg);
dbms_output.put_line('OUTER LOOP SHOWING OUTER'||out_msg||out_ans);
/* Note that when I try to show inner definitions here, in_msg and
in_ans it crashes */
END;
/
SET SERVEROUTPUT OFF
SQL> @ nest3_intro
DECLARE
*
ERROR at line 1:
ORA-06550: line 24, column 64:
PLS-00201: identifier 'V_SECOND' must be declared
ORA-06550: line 24, column 5:
PL/SQL: Statement ignored