Collection methods
Oracle provides set of inbuilt methods, which can be used with a non null collection (except EXISTS), to yield some information and perform basic operations on the collection subscript and element. The list below shows the collections available and their applicability with the different collection types. If the collection is NULL, system raises COLLECTION_IS_NULL exception.
The collection information includes their null behavior of the collection, and element count. The basic utilities provided by Oracle are collection extension, deletion, and trimming.
All collection methods are compiled along with their behavior, in the below table.

Syntax [4]
Sample Code
collection_name.method_name[(parameters)]
Copyright exforsys.com
These collection methods can be used as PL/SQL construct. Oracle does not support the collection methods in SQL.
Now, we shall study the usage of collection methods.
EXISTS
The method checks a collection for the NULL property. It returns FALSE for null collections while TRUE for initialized ones. It is the only collection method which is capable to work with NULL collections.
Note that if the input index value is out of range of the collection, system raises SUBSCRIPT_OUTSIDE_LIMIT exception.
Check the below illustration to demonstrate its use.
Example [7a]
Sample Code
IF EX_NEST_TAB.EXISTS (5) THEN
...
ELSE
END IF;
Copyright exforsys.com
COUNT
The method counts the number of elements in a collection. Check the below illustration.
Check the below illustration to demonstrate the use of COUNT method.
Example [7b]
Sample Code
DECLARE
TYPE T IS TABLE OF NUMBER;
L T := T();
BEGIN
FOR I IN 1..100
LOOP
L.EXTEND;
L(L.LAST):=NULL;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The Collection has '||to_char(L.COUNT)||' elements');
END;
/
The Collection has 100 elements
PL/SQL block successfully completed.
Copyright exforsys.com
LIMIT
The method returns the maximum count of elements in a collection array. It can only be used with VARRAYs.
Note the difference between COUNT and LIMIT methods in the below example.
Example [7c]
Sample Code
DECLARE
TYPE v IS VARRAY(10) OF NUMBER;
M V := V();
BEGIN
M := V(1,3,2,3,2,3);
DBMS_OUTPUT.PUT_LINE('The Collection has '||to_char(M.COUNT)||' elements');
DBMS_OUTPUT.PUT_LINE('The Collection can accommodate '||to_char(M.LIMIT)||' elements');
END;
/
The Collection has 6 elements
The Collection can accommodate 10 elements
PL/SQL procedure successfully completed.
Copyright exforsys.com
FIRST and LAST
The methods return the first and last subscript of a collection. For an uninitialized or empty collection, the method values are NULL. Check the below illustration to demonstrate its use of the methods.
Example [7d]
Sample Code
DECLARE
TYPE T IS TABLE OF NUMBER;
M T := T();
BEGIN
DBMS_OUTPUT.PUT_LINE('FIRST and LAST subscripts for Empty collection are '||M.FIRST||' and '||M.LAST);
M := T(272,4720,482,183,481,372,482,127,5943);
DBMS_OUTPUT.PUT_LINE('FIRST and LAST subscripts for populated collection are '||M.FIRST||' and '||M.LAST);
END;
/
FIRST AND LAST subscripts FOR Empty collection are AND
FIRST AND LAST subscripts FOR populated collection are 1 AND 9
PL/SQL procedure successfully completed.
Copyright exforsys.com
PRIOR and NEXT
The method give the subscript which is before and after the current subscript. These methods are used as to avoid NO_DATA_FOUND exception in sparse collections.
Example [7e]
Sample Code
DECLARE
TYPE T IS TABLE OF NUMBER;
M T := T(12,15,73,1,87,22,89);
BEGIN
DBMS_OUTPUT.PUT_LINE('Subscript before the 5th element is:'||TO_CHAR(M.PRIOR(5)));
DBMS_OUTPUT.PUT_LINE('Subscript after the 5th element is:'||TO_CHAR(M.NEXT(5)));
END;
/
Subscript before the 5th element IS:4
Subscript after the 5th element IS:6
PL/SQL procedure successfully completed.
Copyright exforsys.com
EXTEND
The method allows addition of new elements to a persistent collection. By default, it appends one element to a collection. Upon specification, it can add N elements to a collection. EXTEND(m,n) make m copies of nth element to a collection.
Note that is cannot be used for Associative arrays.
Example [7f]
Sample Code
DECLARE
TYPE T IS TABLE OF NUMBER;
M T := T(12,15,73,1,87,22,89);
BEGIN
DBMS_OUTPUT.PUT_LINE('No of elements in Collection:'|| TO_CHAR(M.COUNT));
M.EXTEND;
DBMS_OUTPUT.PUT_LINE('No of elements in Collection after first extension:'|| TO_CHAR(M.COUNT));
M.EXTEND(5);
DBMS_OUTPUT.PUT_LINE('No of elements in Collection after second extension:'|| TO_CHAR(M.COUNT));
M.EXTEND(2,3);
DBMS_OUTPUT.PUT_LINE('No of elements in Collection after third extension:'|| TO_CHAR(M.COUNT));
END;
/
No of elements IN Collection:7
No of elements IN Collection after first extension:8
No of elements IN Collection after second extension:13
No of elements IN Collection after third extension:15
PL/SQL procedure successfully completed.
Copyright exforsys.com
TRIM
As the name suggests, the method trims a persistent collection. By default, it trims single element in a collection. It can also trim N elements, if the count is specified with the method/<.p>
Note that is cannot be used for Associative arrays.
Example [7g]
Sample Code
DECLARE
TYPE T IS TABLE OF NUMBER;
M T := T(12,15,73,1,87,22,89);
BEGIN
DBMS_OUTPUT.PUT_LINE('Elements in Collection:'|| TO_CHAR(M.COUNT));
M.TRIM;
DBMS_OUTPUT.PUT_LINE('Elements in Collection after first trim:'|| TO_CHAR(M.COUNT));
M.TRIM(5);
DBMS_OUTPUT.PUT_LINE('Elements in Collection after second trim:'|| TO_CHAR(M.COUNT));
END;
/
Elements IN Collection:7
Elements IN Collection after first trim:6
Elements IN Collection after second trim:1
PL/SQL procedure successfully completed.
Copyright exforsys.com
DELETE
The method drops a cell from a collection. It removes a single element by default, but can remove N elements and even by range, if specified with the method.
Example [7h]
Sample Code
DECLARE
TYPE T IS TABLE OF NUMBER;
M T := T(12,15,73,1,87,22,89);
BEGIN
DBMS_OUTPUT.PUT_LINE ('Count of elements before deletion:'||TO_CHAR(M.COUNT));
DBMS_OUTPUT.PUT_LINE ('Last index of elements before deletion:'||TO_CHAR(M.LAST));
M.DELETE(2);
DBMS_OUTPUT.PUT_LINE ('Count of elements after deletion:'||TO_CHAR(M.COUNT));
DBMS_OUTPUT.PUT_LINE ('Last subscript of elements after deletion:'||TO_CHAR(M.LAST));
END;
/
Count of elements before deletion:7
Last INDEX of elements before deletion:7
Count of elements after deletion:6
Last INDEX of elements after deletion:7
PL/SQL procedure successfully completed.
Copyright exforsys.com
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.
H I D E