In Oracle PL/SQL, The Blocks are two types.
1) Anonymous Block
2) Named Block
For the above two blocks the structure is as follows
DECLARE
-- DECLARATION SECTION
BEGIN -- BEGINNING THE PL/SQL BLOCK
--MAIN LOGIC OR BUSINESS LOGIC
BEGIN
--INNER BLOCK OR NESTED BLOCK
END
EXCEPTION SECTION
--ERROR HANDLING SECTION
END -- END OF THE BLOCK
Note: In the above structure we have used -- (two hyphen symbols used to comment the line. For multiline commenting we need to use /* */ )
1) ANONYMOUS BLOCK
In this block the PL/SQL block do not have name. So, The Oracle Engine could not store the Block for the future use.
In realtime environment we mostly do not use the anonymous blocks.
For testing the result manually (Not Recommended for testing We use debugging feature for testing)
or For one time operation we will use the anonymous blocks in PL/SQL.
Example for Anonymous Block:
SQL> SET SERVEROUTPUT ON;
The above statement will enable the console to show the result in commandline.
SQL>
DECLARE
NAME VARCHAR2(30) := 'KIRAN';
BEGIN
DBMS_OUTPUT.PUT_LINE('NAME IS: ' || NAME);
EXCEPTION WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR OCCURED IN THE PL/SQL BLOCK');
END;
2) NAMED BLOCK
As the name suggests the Named block has identified with a name in Oracle. To create the named block we use functions, Triggers, Procedures, Packages etc.
So, The named block have one name and it will be stored permanantly in oracle database. In future we can retrieve the block by identifying with the name
and change the logic.
Example for Named Block: Creating Function
SQL>
CREATE OR REPLACE FUNCTION SUM_FUN(P_NBR1 IN NUMBER,P_NBR2 IN NUMBER)
RETURN NUMBER
AS
V_SUM NUMBER(10);
BEGIN
V_SUM := P_NBR1 + P_NBR2;
RETURN V_SUM;
END;
SQL> SELECT SUM_FUN(10, 20) AS TWO_NUM_SUM FROM DUAL;
In the above select statement we are using the named block i.e Function with the name SUM_FUN by passing two input parameters 10 and 20.
So the result is 30 ( i.e 10 + 20)
Note: The named blocks i.e functions, procedures, packages and triggers will be discussed in future tutorials.
1) Anonymous Block
2) Named Block
For the above two blocks the structure is as follows
DECLARE
-- DECLARATION SECTION
BEGIN -- BEGINNING THE PL/SQL BLOCK
--MAIN LOGIC OR BUSINESS LOGIC
BEGIN
--INNER BLOCK OR NESTED BLOCK
END
EXCEPTION SECTION
--ERROR HANDLING SECTION
END -- END OF THE BLOCK
Note: In the above structure we have used -- (two hyphen symbols used to comment the line. For multiline commenting we need to use /* */ )
1) ANONYMOUS BLOCK
In this block the PL/SQL block do not have name. So, The Oracle Engine could not store the Block for the future use.
In realtime environment we mostly do not use the anonymous blocks.
For testing the result manually (Not Recommended for testing We use debugging feature for testing)
or For one time operation we will use the anonymous blocks in PL/SQL.
Example for Anonymous Block:
SQL> SET SERVEROUTPUT ON;
The above statement will enable the console to show the result in commandline.
SQL>
DECLARE
NAME VARCHAR2(30) := 'KIRAN';
BEGIN
DBMS_OUTPUT.PUT_LINE('NAME IS: ' || NAME);
EXCEPTION WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR OCCURED IN THE PL/SQL BLOCK');
END;
2) NAMED BLOCK
As the name suggests the Named block has identified with a name in Oracle. To create the named block we use functions, Triggers, Procedures, Packages etc.
So, The named block have one name and it will be stored permanantly in oracle database. In future we can retrieve the block by identifying with the name
and change the logic.
Example for Named Block: Creating Function
SQL>
CREATE OR REPLACE FUNCTION SUM_FUN(P_NBR1 IN NUMBER,P_NBR2 IN NUMBER)
RETURN NUMBER
AS
V_SUM NUMBER(10);
BEGIN
V_SUM := P_NBR1 + P_NBR2;
RETURN V_SUM;
END;
SQL> SELECT SUM_FUN(10, 20) AS TWO_NUM_SUM FROM DUAL;
In the above select statement we are using the named block i.e Function with the name SUM_FUN by passing two input parameters 10 and 20.
So the result is 30 ( i.e 10 + 20)
Note: The named blocks i.e functions, procedures, packages and triggers will be discussed in future tutorials.
No comments:
Post a Comment