Nested Tables: PL/SQL Example1
Nested Tables: PL/SQL Example1
A Nested table is like a database table which has no limit on its size. Elements are inserted
into nested table starting with index 1. Nested table can be created in both SQL and PLSQL.
Nested table is nothing but a table with in a table.
A table is represented as a column with in another table.
There is no limit to the number of rows in the nested table for each row in the main table.
Basically it is used for mapping master-detail relationship between tables.
We would use Nested tables when physical size of the collection is unknown and wanted to
extend it dynamically.
We can use Nested Tables in SQL as well as PL/SQL.
Extend is a method that adds a new empty element to a nested table, this must be called
first.
We cannot delete individual elements from a Varray. But we can delete elements from a
nested table using the built-in procedure DELETE. That might leave gaps in the index, but the built-
in function NEXT lets you iterate over any series of subscripts.
PL/SQL Example1:-
[sql]DECLARE
/* declare the table type */
TYPE students_table IS TABLE OF VARCHAR2 (10);
/* calls the nested table constructor */
students students_table := students_table ();
BEGIN
students.EXTEND;
/* can have up to 10 characters as stated above*/
students (1):= ‘SCOTT’;
students.EXTEND;
students (2):= ‘LUCAS’;
students.EXTEND;
students (3):= ‘SMITH’;
/* print the table type elements by using FIRST and LAST */
FOR i IN students.FIRST .. students.LAST
LOOP
DBMS_OUTPUT.put_line (students (i));
END LOOP;
END;[/sql]
Output:-
Example2:-
[sql]DECLARE
/*define a table type of “emp” type and we are not specify the size of the table*/
TYPE emptype IS TABLE OF emp%ROWTYPE;
/*declare and initialize a null set of rows*/
emptab emptype := emptype ();
/* declare the cursor based on “emp” table */
CURSOR c_emp
IS
SELECT * FROM emp;
v_num NUMBER;
BEGIN
v_num:= 1;
FOR i IN c_emp
LOOP
/*initialize row*/
emptab.EXTEND;
SELECT * INTO emptab (v_num)
FROM emp
WHERE empno = i.empno;
/*print the contents*/
DBMS_OUTPUT.put_line (emptab (v_num).empno || ‘ ‘|| emptab (v_num).ename);
v_num := v_num+ 1;
END LOOP;
END;[/sql]
Output:–
7839 KING
7698 BLAKE
7782 CLARK
7499 ALLEN
7521 WARD
7654 MARTIN
7844 TURNER
7900 JAMES
7934 MILLER
Common Exceptions in Nested Tables:-
Example3:-
[sql]DECLARE
/* define a Nested Table */
TYPE char_type IS TABLE OF VARCHAR2 (10);
/*we declare a Nested table variable but not initialize to that so system will raise an error*/
c_list char_type;
BEGIN
/* allocates space in the Nested Table */
c_list.EXTEND;
c_list (1):= ‘SCOTT’;
/* Can have up to 10 characters */
c_list.EXTEND;
c_list (2) := ‘TIGER’;
/* print the Nested table elements by using its methods FIRST and LAST */
FOR i IN c_list.FIRST.. c_list.LAST
LOOP
DBMS_OUTPUT.put_line (c_list (i));
END LOOP;
END;[/sql]
Output:-
Error report:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 8
This exception raise when you forgot to initialized the Nested Table. So once declare the Nested
Table and initialize the variable is must otherwise system will rise above exception.
Example4:-
[sql]DECLARE
/* define a Nested Table*/
TYPE char_type IS TABLE OF VARCHAR2 (10);
c_list char_type:=char_type ();
BEGIN
c_list.EXTEND;
c_list (1):= ‘SCOTT’;
/* Can have up to 10 characters */
c_list.EXTEND;
c_list (2):= ‘TIGER’;
/* before allocates space in the Nested Table we try to use that variable then system will raise
an error */
c_list (3):= ‘KING’;
/* print the Nested Table elements by using its methods FIRST and LAST */
FOR i IN c_list.FIRST.. c_list.LAST
LOOP
DBMS_OUTPUT.put_line (c_list (i));
END LOOP;
END;[/sql]
Output:-
Error report:
ORA-06533: Subscript beyond count
ORA-06512: at line 12
The exception means that subscript 3 is unavailable. It does not exist. While you defined the Nested
table having some elements but we are not specify the size, then you allocates the space for only
two items and third one we can not allocate the space. Therefore the variable has only two valid
subscripts, one and two.