PLSQL Nested Tables
PLSQL Nested Tables
As you know, a datatype is used to classify the data to be stored as number, varchar,
boolean etc. But a varible declared using a dataype can hold only a single data value at a
time, i.e a single memory location.
What if we need to store multiple rows of data in a single variable? There comes the use
of collections in Oracle. A variable declared as a collection could store an
array of data of the same TYPE or ROWTYPE. This is beneficial when we need to pass
multiple rows of data between Procedures or need to return multiple records
from functions especially to other languages like Java, C etc.
Here I am discussing PL/SQL TABLE (index-by tables) and NESTED TABLE in detail
These are single-column tables and could be considered as an Array of data but
which is unbounded; i.e we cannot set a limit on number of rows like an array
and that s sometimes an added advantage.
Both PL/SQL TABLE and NESTED TABLE has got the same structure and all, but the
main difference is, nested tables could be stored in a database
column whereas PL/SQL tables could not be. To explain further, Nested table Dataypes
can be created standalone they could be used for columns in normal database Tables.
Declaration:
a. NESTED TABLE
Eg:
CREATE OR REPLACE PROCEDURE PRC_NT
IS
TYPE NTab_List1 is TABLE OF TYPE INTEGER; --create a Collection type TYPE
NTab_List1
TAB NTab_List1; --create a variable of
NTab_List1 Type
Begin
--<code here>
End;
--<code here>
End;
a.3 Declare and use outside PL/SQL code: (Used as Column Datatypes in Tables)
The address of a customer goes into the single column NTab_List. As I said earlier, here
comes the advantage of an unbouded array. The Nested table in the
column provides the flexibility of inserting varying number of address lines into the
column. The column ADDRESS could have any number of records in a single cell.
BEGIN
INSERT INTO NS_TEST
VALUES(1001,'Lynn Saunders',
NTab_List ( 1027 ,
North Avenue ,
Regent Street ,
Nt Valley ,
Berligton
74839 )
);
END;
DECLARE
NTAB NTAB_LIST;
BEGIN
SELECT NTAB_LIST INTO NTAB FROM NS_TEST
WHERE ID = 1001;
END;
III. Updation
Suppose the customer with id=1001 needs to change his address. Then
DECLARE
NEW_NTAB NTAB_LIST:=
NTAB_LIST ( 892 ,
SOUTH CIRCLE ,
MALLEY STREET ,
89399 );
BEGIN
UPDATE DEPT
SET NTAB_LIST = NEW_NTAB WHERE ID=1001;
END;
b. PL/SQL TABLE:
The difference is PL/SQL table should always be indexed any of the below
types.
BINARY_INTEGER, BOOLEAN, LONG, LONG RAW, NATURAL, NATURALN,
PLS_INTEGER, POSITIVE, POSITIVEN, SIGNTYPE, and STRING.
Declaration:
Also, we can use the %TYPE to make use of the dynamic datatype
functionality.
For eg:
To extend a Nested table, you need to use a built-in procedure EXTEND, but to extend a
PL/SQL table, you just increment the subscripts.
The value of an uninitialized Nested table is NULL, but an uninitialized PL/SQL table is
empty. So, you can compare nested tables using IS NULL operator.
PL/SQL tables are initially sparse. They can store reference data using a numeric primary
key as the index.
PL/SQL supports implicit (automatic) datatype conversion between Arrays and PL/SQL
tables.