Introduction
I believe that you have successfully completed day 1 of learning basics of PL/SQL Oracle from scratch in seven Days. We will get further into learning more about Oracle PL/SQL.
What we learned last time
On day One we learned the following things:
- Understand the features of PL/SQL
- Understand the PL/SQL Block structure
- Recognize various PL/SQL types, variables and constants
- Create an Anonymous PL/SQL Block
- Printing value of a variable using Anonymous Block
Agenda for Day Two
- Nesting of Anonymous Block into another
- PL/SQL Data Types
In the last session we created a small anonymous block as shown below. Now we will try to nest one more anonymous block inside that.
- DECLARE --DECLRATION //OUTER BLOCK
- l_count NUMBER; --DECLARING VARIABLE
- Begin --BEGIN BLOCK
- l_count:=3; --INITIALIZATION
- DECLARE --NESTED BLOCK INNER BLOCK
- i_num NUMBER;
- Begin
- i_num:=2*2;
- DBMS_OUTPUT.PUT_LINE('The value of i_num is ' ||i_num);--PRINTING VALUE OF l_count
- Exception whenothers thennull;
- END;--NESTED INNER BLOCK ENDS HERE
- DBMS_OUTPUT.PUT_LINE('The value of variable l_count is ' ||l_count);--PRINTING VALUE OF l_count
- EXCEPTION --TRAPPING THE EXCEPTION
- WHEN OTHERS THEN
- NULL;
- END;--END OF ANONYMOYS BLOCK
I have DBMS_OUTPUT.PUT_LINE in my previous article as well let’s focus on what that actually means.
DBMS_OUTPUT.PUT_LINE is used to display information from a PL/SQL Block,
- DBMS_OUTPUT is a package,
- PUT_LINEis a procedure within that package.
Note: If you are using SQL plus then DBMS_OUTPUT Package needs to be enabled by the following command.
SET SERVEROUTPUT ON;
Let’s try this using SQLPLUS without setting SERVEROUTPUT ON

Figure1: Demonstrating enabling DBMS_OUTPUT in SQLPLUS
We can see the above as I try to execute the anonymous block, it gets executed successfully but no DBMS_OUTPUT.PUT_LINE was shown. Now we will SET SERVEROUTPUT ON and try if now output comes to the screen or not.

Figure 2: DBMS_OUTPUT.PUT_LINE Example
We can clearly see the output shown as 10. So if we want to enable the DBMS_Output package we need to enable it using SET SERVEROUTPUT ON;
BLOCK Label
We can access the block label to access the variables of the block by the help of qualifier i.e. have two variables declared with the same name in the outside block and inside the nested block but we want to access the outside block variable inside the inner and block we can accomplish the same using Qualifier as shown below:
- <<parent>> --label Qualifier
- DECLARE --DECLRATION //OUTER BLOCK
- i_num NUMBER; --DECLARING VARIABLE
- Begin --BEGIN BLOCK
- i_num:=3; --INITIALIZATION
- DECLARE --NESTED BLOCK INNER BLOCK
- i_num NUMBER;
- Begin
- DBMS_OUTPUT.PUT_LINE('The value of parent i_num is ' ||parent.i_num); --accessing outer block variable
- i_num:=2*2;
- DBMS_OUTPUT.PUT_LINE('The value of inner i_num is ' ||i_num);--PRINTING VALUE OF l_count
- Exception when
- others then
- null;
- END;
- DBMS_OUTPUT.PUT_LINE('The value of variable i_num is ' ||i_num);--PRINTING VALUE OF l_count
- EXCEPTION --TRAPPING THE EXCEPTION
- WHEN OTHERS THEN
- NULL;
- END;--END OF ANONYMOYS BLOCK

Figure 3: Scope of the variable
PL/SQL Data Types
The PL/SQL data types are divided into two parts:
- Scalar Data Types: It is used to hold single value number, varchar2, date etc.
- Composite Data Types: It is used to hold one or more item of same type or dissimilar type. PL/SQL Records.
- Reference Types: Reference type is similar to C pointer concepts and can shortly be defined as Cursor in oracle.
- LOB(Large Objects): A large object can either be a binary or character value up to 4gb, for example, BLOB, CLOB, NCLOB and BFILE.
Scalar Data Type
Scalar Data types are the basic data types, these data types can be subdivided into 4 parts,
- Numeric
- Character
- Date & Time
- Boolean
Numeric Data type then can be subdivided into NUMBER data type, PLS/INTEGER OR BINARY INTEGER TYPE OR BINARY FLOAT OR BINDARY DOUBLE Data type.
Number Data type is the most commonly used data type in oracle when we are performing arithmetic operations etc.
Number Data type is portable across all Oracle platforms. Number data type can be defined as fixed point numbers or as floating point numbers.
Fixed point Number: We declare the fixed limit of the type which is:
- Precision: Total number of digits in the number, max value is 38.
- Scale: Round number or decimal places has min value of -84 and max value 127.
- DECLARATION
- NUMBER[(precision,scale)]
Example:
Figure 4: Demonstrating number precision and scale
21.25 can easily be saved inside the v_num which has 4 as precision and 2 as scale. We will get the output as shown below:

Figure 5: Demonstrating number with 4,2
If I try to change the value to 132.25, I will get an exception as v_num is not able to hold 5 precision. Let’s try this practically.

Figure 6: Exception when we try to store more than the declared precision and scale
So we can see the below output of the above test. You would be thinking what is Sqlcode and SQLERM which I will dicuss in the next article.
Floating Points Number
Numbers are also used to represent floating point numbers i.e. we are not defining the precision and scale. Now the number data type we define is capable of holding all these numbers varying the number of total digits or after the decimal points; let us try it practically.

Figure 7: Demonstrating floating point variable
Here in the above figure we can clearly see that a NUMBER data type can easily represent the floating point numbers.
Constant: Data types are used to hold values which remain the same during the execution of PL/SQL.
- Values cannot be changed
- Constant is defined in the declaration section














Bhuvanesh MohankumarPosted May 3, 2016, 11:44 AM
Good one...
kalu singh raoPosted Mar 28, 2016, 11:08 AM
Nice...
Mohammed IbrahimPosted Mar 28, 2016, 4:10 AM
nice
Sibeesh VenuPosted Mar 28, 2016, 2:09 AM
Nice Share
Vignesh ManiPosted Mar 27, 2016, 3:42 PM
Nice