--1st Scenario
SET SERVEROUTPUT ON
DECLARE
v_weight NUMBER(3):=600;
v_message VARCHAR2(255):=' Product 10012';
v_new_locn VARCHAR2(50) :='America';
BEGIN
DECLARE
v_weight NUMBER(3) :=1;
v_message VARCHAR2(255):='Product 11001';
v_new_locn VARCHAR2(50) :='Europe';
BEGIN
v_weight :=v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn:='Western ' || v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
v_weight := v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
v_message:=v_message || ' is in stock';
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn :='Eastern '||v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
1st Output:
2
Product 11001
Western Europe
601
Product 10012 is in stock
Eastern America
*******************************xxxxxxxxxxxx****************************************
2nd Scenario:
-We can access a variable from outer block whether it is declared or not in the inner block but not vice versa.
--Inner Block executes once before the outer block.
--The scope of the variable ends within the BEGIN ... END statement of the inner block.
DECLARE
v_weight NUMBER(3):=600;
v_message VARCHAR2(255):=' Product 10012';
v_new_locn VARCHAR2(50) :='America';
BEGIN
DECLARE
v_message VARCHAR2(255):='Product 11001';
v_new_locn VARCHAR2(50) :='Europe';
--v_weight NUMBER(3) :=1;
/*v_weight variable commented out in the inner block to see the difference by not declaring in inner
block but calling it from outer block */
BEGIN
v_weight :=v_weight+1; /* taking value from outer block and incremented */
DBMS_OUTPUT.PUT_LINE(v_weight);
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn:='Western ' || v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
v_weight := v_weight+1; /* taking value derived from inner block and further incremented as inner block executes once before outer block*/
DBMS_OUTPUT.PUT_LINE(v_weight);
v_message:=v_message || ' is in stock';
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn :='Eastern '||v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
2nd Output:
601
Product 11001
Western Europe
602
Product 10012 is in stock
Eastern America
*********************************xxxxxx**********************************
--3rd Scenario
DECLARE
v_weight NUMBER(3):=600;
v_message VARCHAR2(255):=' Product 10012';
BEGIN
DECLARE
v_weight NUMBER(3) :=1;
v_message VARCHAR2(255):='Product 11001';
v_new_locn VARCHAR2(50) :='Europe';
BEGIN
v_weight :=v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn:='Western ' || v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
v_weight := v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
v_message:=v_message || ' is in stock';
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn :='Eastern '||v_new_locn; /* We can't call v_new_locn from inner block as it is not declared in outer block*/
/*v_new_locn is not visible in the main block and you would see an error*/
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
compilation error:
PLS-00201: identifier 'V_NEW_LOCN' must be declared
ORA-06550: line 23, column 1:
PL/SQL: Statement ignored
Cheers
Rajani
SET SERVEROUTPUT ON
DECLARE
v_weight NUMBER(3):=600;
v_message VARCHAR2(255):=' Product 10012';
v_new_locn VARCHAR2(50) :='America';
BEGIN
DECLARE
v_weight NUMBER(3) :=1;
v_message VARCHAR2(255):='Product 11001';
v_new_locn VARCHAR2(50) :='Europe';
BEGIN
v_weight :=v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn:='Western ' || v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
v_weight := v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
v_message:=v_message || ' is in stock';
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn :='Eastern '||v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
1st Output:
2
Product 11001
Western Europe
601
Product 10012 is in stock
Eastern America
*******************************xxxxxxxxxxxx****************************************
2nd Scenario:
-We can access a variable from outer block whether it is declared or not in the inner block but not vice versa.
--Inner Block executes once before the outer block.
--The scope of the variable ends within the BEGIN ... END statement of the inner block.
DECLARE
v_weight NUMBER(3):=600;
v_message VARCHAR2(255):=' Product 10012';
v_new_locn VARCHAR2(50) :='America';
BEGIN
DECLARE
v_message VARCHAR2(255):='Product 11001';
v_new_locn VARCHAR2(50) :='Europe';
--v_weight NUMBER(3) :=1;
/*v_weight variable commented out in the inner block to see the difference by not declaring in inner
block but calling it from outer block */
BEGIN
v_weight :=v_weight+1; /* taking value from outer block and incremented */
DBMS_OUTPUT.PUT_LINE(v_weight);
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn:='Western ' || v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
v_weight := v_weight+1; /* taking value derived from inner block and further incremented as inner block executes once before outer block*/
DBMS_OUTPUT.PUT_LINE(v_weight);
v_message:=v_message || ' is in stock';
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn :='Eastern '||v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
2nd Output:
601
Product 11001
Western Europe
602
Product 10012 is in stock
Eastern America
*********************************xxxxxx**********************************
--3rd Scenario
DECLARE
v_weight NUMBER(3):=600;
v_message VARCHAR2(255):=' Product 10012';
BEGIN
DECLARE
v_weight NUMBER(3) :=1;
v_message VARCHAR2(255):='Product 11001';
v_new_locn VARCHAR2(50) :='Europe';
BEGIN
v_weight :=v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn:='Western ' || v_new_locn;
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
v_weight := v_weight+1;
DBMS_OUTPUT.PUT_LINE(v_weight);
v_message:=v_message || ' is in stock';
DBMS_OUTPUT.PUT_LINE(v_message);
v_new_locn :='Eastern '||v_new_locn; /* We can't call v_new_locn from inner block as it is not declared in outer block*/
/*v_new_locn is not visible in the main block and you would see an error*/
DBMS_OUTPUT.PUT_LINE(v_new_locn);
END;
compilation error:
PLS-00201: identifier 'V_NEW_LOCN' must be declared
ORA-06550: line 23, column 1:
PL/SQL: Statement ignored
Cheers
Rajani
No comments:
Post a Comment