Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Collection methods

Collection methods are PL/SQL’s in-built functions and procedures which can be used in
conjunction with collections.
Using PL/SQL collection method you can get the information as well as alter the content of
the collection.

In Oracle Database we have 3 Collection Procedures and 7 Collection functions. In total we


have 10 collection methods. Here are their names —
Collection Functions
 Count
 Exists
 First, Last
 Limit
 Prior, Next
Collection Procedures
 Delete
 Extend
 Trim

In Oracle PL/SQL, collection methods can be used using Dot (.) notation. syntax.

Collection. Method (parameters)

===========================================================================

Collection Method: COUNT

--Q. What is Collection Method COUNT ( )?


Collection method COUNT ( ) returns the number of elements in an initialize collection.
If used with an initialize collection with no elements; it returns zero.

--Q. When does Collection Method COUNT ( ) return zero?


Collection method COUNT ( ) returns zero when it is applied or say used with an initialize
collection
(i.e. VARRAYs & Nested Tables) with no elements.
It also returns zero as a result when it is used with an empty associated array.

--Q. Does collection method COUNT ( ) works the same with a Nested Table?
No. This is because COUNT ( ), returns the number of non-empty elements in a nested table
since it is possible for a collection nested table to have individual elements that are empty.

--Q. Why the heck am I getting ‘Collection_IS_NULL’ error?


It seems like you are using COUNT ( ) with an uninitialized collection.
Whenever you apply collection function COUNT ( ) to an uninitialized collection (i.e. Nested
Tables & VARRAYs) it raises the ‘Collection_Is_Null’
exception which is a pre-defined exception in Oracle Database.

As Associative Arrays don’t require initialization, thus you will not get this exception with
them.
===========================================================================
-- Examples of Collection Method COUNT ( )

Example 1: Compute total number of elements stored in a Nested table.


You can use COUNT ( ) function to compute total number of elements stored into a
collection such as Nested table.

DECLARE
TYPE my_nested_table IS TABLE OF number;
var_nt my_nested_table := my_nested_table (9,18,27,36,45,54,63,72,81,90);
BEGIN
DBMS_OUTPUT.PUT_LINE ('The Size of the Nested Table is ' ||var_nt.count);
END;

Example 2. COUNT ( ) function with IF Condition


You can use COUNT ( ) function to control the flow of the program using a condition.

DECLARE
TYPE my_nested_table IS TABLE OF number;
var_nt my_nested_table := my_nested_table (9,18,27,36,45,54,63,72,81,90);
BEGIN
IF var_nt.count >= 10 THEN
DBMS_OUTPUT.PUT_LINE (‘you have already inserted 10 elements in your Nested table);
DBMS_OUTPUT.PUT_LINE ('Are you sure you want to insert more?');
END IF;
END;

Example 3. Similarly you can use the collection method COUNT ( ) with Loops.

DECLARE
TYPE my_nested_table IS TABLE OF number;
var_nt my_nested_table := my_nested_table (9,18,27,36,45,54,63,72,81,90);
BEGIN
for i in 1..var_nt.count
loop
dbms_output.put_line ('value stored at index '||i||'is'||var_nt(i));
end loop;
end;
===========================================================================
Collection Method: EXISTS

--Q. What is Collection Method EXISTS ( )?


Collection Method EXISTS ( ) checks the existence of an element at a specific index in a
collection.
If it finds the specified element then it returns TRUE otherwise it returns FALSE.

You can use EXISTS ( ) function to check the existence of a specific row within the collection.

Syntax of Collection Method EXISTS ( )


EXISTS (index number);

EXISTS function takes the subscript/index number of a cell of the collection as an Input and
searches it in the collection.If it finds any element corresponding to index number then it
returns TRUE otherwise it returns FALSE.

--Q. Does the collection method EXISTS returns NULL?


No. EXISTS function does not return null. It either returns True or False.

--Q. What if I remove an already existed row using TRIM or DELETE function?
If you remove a row using Trim or Delete function then collection method EXISTS ( ) will
return FALSE for the index of that row.

--Q. Does the collection method EXISTS raise any exception?


No, collection method EXISTS does not raise any exception. In fact, this is the only function
which does not raise any exception, even if it used with an uninitialized collection.

--Q. If it does not raise an exception then what will happen if I applied this function to an
uninitialized collection?
Collection method EXISTS ( ) returns false, if it is applied either to an uninitialized collection
or to an initialize collection with no elements.

-- Example of Collection Method EXISTS ( ).

DECLARE
TYPE my_nested_table IS TABLE OF VARCHAR2 (20);
var_1 my_nested_table := my_nested_table('Super Man','Iron Man','Bat Man');
BEGIN
IF var_1.EXISTS (1) THEN
DBMS_OUTPUT.PUT_LINE ('Hey we found '|| var_1 (1));
ELSE
DBMS_OUTPUT.PUT_LINE ('Sorry, no data at this INDEX');
END IF;
END;
In the above program we are checking if there is any element at index 1 into the collection
‘my_nested_table’ or not.
If there is an element at the specified index then the IF part of the IF-ELSE statement will
execute otherwise the ELSE part will come into the action.

--Q. What will you do…?


Suppose you want to insert a data into a specific index but you are not sure whether that
index is already holding some data or not.
In case if it is holding any then you don’t want to overwrite it. What will you do in this
case?

declare
type t1 is table of number;
v_t t1 := t1(1,2,3,4,5);
begin
if v_t.exists (6) then
dbms_output.put_line (v_t(6));
else
dbms_output.put_line ('Data is not present you can add the data');
v_t.extend;
v_t(6):=6;
dbms_output.put_line ('New data inserted');
end if;
for i in v_t.first..v_t.last loop
dbms_output.put_line (v_t(i));
end loop;
end;
===========================================================================
Collection Methods: FIRST & LAST

--Q. What are collection methods FIRST ( ) and LAST ( )?


We use collection functions First & Last to know the first and last index values defined in a
collection.

--Q. Can we use these collection methods with any type of collection?
Yes! You can use both these functions with all three types of collections that are Associative
Array, Nested table and VARRAYs.

--Q. When does collection method FIRST ( ) and LAST ( ) return null?
Both the functions return null when applied to an empty collection or when applied to an
initialize collection that has no elements.

--Q. What if there is only 1 element in my VARRAY?


In that case collection function FIRST ( ) is always 1 and collection method LAST ( ) is always
equal to COUNT.
--Q. What if I applied these functions to an uninitialized collection?
if you applied collection function FIRST & LAST to an uninitialized collection then it will raise
COLLECTION_IS_NULL exception.

--Example: How to use collection function FIRST and LAST with collection?

DECLARE
TYPE nt_tab IS TABLE OF NUMBER;
col_var nt_tab := nt_tab(10, 20, 30, 40, 50);
BEGIN
DBMS_OUTPUT.PUT_LINE ('First Index of the Nested table is ' || col_var.FIRST);
DBMS_OUTPUT.PUT_LINE ('Last Index of the Nested table is ' || col_var.LAST);
END;
/
In the above example we have created a nested table with the name NT_TAB and initialized
it using collection variable col_var.
This nested table has 5 indexes into which we have stored the values.
Thus on execution the result from the first DBMS_OUTPUT will be 1 and from the second
DBMS_OUTPUT statement will be 5.

--Q. What if we delete the first element of the nested table? What will then be the output
of collection function FIRST?
If you delete the first element of the collection function then the collection function FIRST
will return the subscript which is greater than 1 and is holding some data.
Let’s see the example:

DECLARE
TYPE nt_tab IS TABLE OF NUMBER;
col_var nt_tab := nt_tab(10, 20, 30, 40, 50);
BEGIN
col_var.DELETE(1);
col_var.DELETE(5);
DBMS_OUTPUT.PUT_LINE ('First Index after DELETE is ' || col_var.FIRST);
DBMS_OUTPUT.PUT_LINE ('Last Index after DELETE is ' || col_var.LAST);
END;
/

OUTPUT- First Index after DELETE is 2.


Last Index after DELETE is 4

After deleting the first element which is 10 on index 1 the new lowest subscript is now 2
which has some data stored into it. Thus on execution the result will be 2.
--Q. What if I delete the element from the middle of the collection?
If you delete the data from the middle then the collection function LAST will return a value
which is greater than the value returned by COUNT method.

--Q. Can we see the data stored into the indexes of the collection using FIRST and LAST
collection methods?
yes! Of course. You can use these functions to see the data stored in lowest and highest
index of the collection.

--For example

DECLARE
TYPE nt_tab IS TABLE OF NUMBER;
col_var nt_tab := nt_tab(10, 20, 30, 40, 50);
BEGIN
DBMS_OUTPUT.PUT_LINE ('Value stored at First Index is ' || col_var(col_var.FIRST));
DBMS_OUTPUT.PUT_LINE ('Value stored at First Index is ' || col_var(col_var.LAST));
END;

OUTPUT- Value stored at First Index is 10


Value stored at First Index is 50

In order to see the data stored into the first and last index you just have to place the
function calls of these function inside
the parenthesis of collection variable which is col_var just like we did in the above example.
===========================================================================
Collection Method: LIMIT

--Q. What is Collection Method LIMIT?


Collection method LIMIT which is actually a PL/SQL function returns the maximum number
of elements that a VARRAY can hold.
This means that by using this function you can find out how many elements you can store in
a VARRAY. limits reads indexes which are created by given VARRAYS size limit.

--Q. What does collection method LIMIT return?


Collection method LIMIT return a value of PLS_INTEGER type.

--Q. Does this function works with other two collection Nested Tables and Associative
Array also?
Collection method LIMIT only works with VARRAY. If it applied to Nested table or
associative array then this function will either return a NULL or No value. So the answer is,
No, the collection function LIMIT does not work with Nested tables and Associative Arrays.
--Q. Does the collection function LIMIT raises any exception? If yes, then when?
Yes the function LIMIT raises COLLECTION_IS_NULL exception if it is applied to an
uninitialized nested table or a VARRAY.

-- example of how to use the function LIMIT?


DECLARE
TYPE t1 IS VARRAY (5) OF NUMBER;
V_t t1 := t1();
BEGIN
--Let's find out total number of indexes in the above VARRAY
DBMS_OUTPUT.PUT_LINE ('Total Indexes '||v_t.LIMIT);
END;

-- Don’t we have the function COUNT which gives the same information?
The collection function LIMIT returns the total number of indexes of a VARRAY regardless of
whether these indexes are empty or holding some data.

While the collection function COUNT returns the number of Indexes which are not empty
and holding some data.

DECLARE
TYPE t1 IS VARRAY ( 5 ) OF NUMBER;
V_t t1 := t1 ();
BEGIN
V_t.extend;
v_t(1) := 10 * 2;
dbms_output.put_line('Total Number of Index ' || v_t.limit);
dbms_output.put_line('Total Number of Index which are occupied ' || v_t.count);
END;

In the above code we have a VARRAY which is capable of holding 5 elements of NUMBER
datatype. Result of LIMIT function will return 5 because that is the total strength of our
VARRAY while the count function return 1 because among those 5 indexes there is only one
index which has some data stored into it.

--Example-Finding out the number of vacant index for your use in a VARRAY is very easy.
If you subtract the result of count function from the result of the LIMIT function you will get
the total number of elements left un-used for you to store data into a varray.

DECLAR
TYPE t1 IS VARRAY ( 5 ) OF NUMBER;
V_t t1:= t1 ();
BEGIN
v_t.extend;
v_t (1) := 10 * 2;
dbms_output.put_line('Total Number of Index ' || v_t.limit);
dbms_output.put_line('Total Number of Index which are occupied ' || v_t.count);
dbms_output.put_line('Total Number of Vacant index left for use '
|| (v_t.limit - v_t.count) );
END;

===========================================================================
Collection Method: PRIOR & NEXT

--Q. What are Prior and Next Collection Functions?


PL/SQL collection method PRIOR takes an index as input and returns the value stored into
the previous lowest index.
Whereas the collection method NEXT returns the value from the next higher index.

--Q. Can we use both these functions with all three types of collections?
Yes, both Prior and Next collection functions can be used with all the three types of
collections.

--Q. When will PL/SQL Collection Methods Prior and Next return null?
Collection Method Prior returns null when there are no lower subscript values available and
Collection method Next returns null when there are no higher subscript values available to
return.

--Q. What will be the output of Collection method Next and Prior if we use them with
associative array?
If collection method PRIOR and NEXT are used with associative arrays then they will return
an output of VARCHAR2 or LONG datatype.

--Q. these methods raise some type of exception. Is it true?


Yes, it is true. If either of these functions are applied to an uninitialized Nested Table or a
Varray then they
raise the COLLECTION_IS_NULL exception.

--Example of Collection Method Prior.


DECLARE
TYPE nested_t1 IS TABLE OF NUMBER;
v_t nested_t1:= nested_t1 (9,18,27,36,45,54,63,72,81,90);
BEGIN
dbms_output.put_line('Index prior to index 3 is '|| v_t.PRIOR(3));
dbms_output.put_line('Value before 3rd Index is '|| v_t (v_t.PRIOR(3)));
END;

Output-
Index prior to index 3 is 2
Value before 3rd Index is 18
The first output statement will return the index number prior to the Index number 3 which
has some value stored into it.
In the second output statement we called the Prior function and supplied it as an input to
the collection object.

var_nt(var_nt.PRIOR(3))

--Q. What will happen if we delete the Previous Lowest Index from the nested table?
In that case, result will definitely not be the same. Prior function returns the previous
lowest index. But that Index must contain some value.

DECLARE
TYPE my_nested_table IS TABLE OF NUMBER;
var_nt my_nested_table := my_nested_table(9,18,27,36,45,54,63,72,81,90);
BEGIN
var_nt.DELETE(2);
dbms_output.put_line('Index prior to index 3 is '||var_nt.PRIOR(3));
dbms_output.put_line('Value before 3rd Index is '||var_nt(var_nt.PRIOR(3)));
END;
/
Output-
Prior index is:1
Prior index is:9

-- Example of Collection Method Next.


Collection method NEXT returns the value from the next higher index.
--example
DECLARE
TYPE my_nested_table IS TABLE OF NUMBER;
var_nt my_nested_table := my_nested_table(9,18,27,36,45,54,63,72,81,90);
BEGIN
dbms_output.put_line('Next Higher Index to index 3 is '||var_nt.NEXT(3));
dbms_output.put_line('Value after 3rd Index is '||var_nt(var_nt.NEXT(3)));
END;
/
Output-
Next Higher Index to index 3 is 4
Value after 3rd Index is 36

The first output statement will return the next non-empty index number while the second
one will return the data stored into that index. The working of both these statements will
be the same as we discussed above.
===========================================================================
Collection Method: DELETE Procedure

we have 7 collection functions and 3 collection procedures.

--Q. What is PL/SQL collection method Delete?


Collection method DELETE is an overloaded procedure which removes elements from the
collection.

--Q. What do you mean by an overloaded procedure?


PL/SQL collection method DELETE is an overloaded procedure.
Which means you can use the same procedure in three different ways.
These three different ways are –

1. DELETE: Simple procedure call without any parameters.


Thus if the PL/SQL Collection procedure DELETE is used without any parameter then
it will remove all the elements from the collection.

2. DELETE (index-number): Procedure call with a single parameter.


Collection procedure DELETE called by passing a valid index number will remove the
element of the specific index.

3. DELETE (start-index, ending-index): Procedure call with two parameters.


This way of calling a DELETE procedure is termed as Range delete.
In this way you have to specify two Indexes. And the procedure deletes the range of
elements which fall between starting-index and ending-index.
If the collection is a string-indexed associative array the starting-index and ending-index
are string; otherwise starting and ending indexes are integers.

--Q. Can we use DELETE Procedure in Oracle Database with all the collections?
Yes, collection method DELETE can be used with all three types of collections.
These are – Nested table, VARRAYs and Associative arrays.

--Q. if we use the procedure DELETE with VARRAYs then won’t it make a sparse
collection?
As VARRAY is not a sparse collection hence we cannot delete individual rows from it.
Furthermore, the only procedure call that we can execute with VARRAY is the first one.
Which is collection method DELETE without any arguments which removes all the elements
from the collection.
The only way of removing an individual row from a VARRAY is by trimming it from its end
using another procedure
call TRIM.

--Q. Can we expect any exception with collection method DELETE?


Yes, there is an exception associated with PL/SQL collection method DELETE.
If the procedure DELETE is applied to an uninitialized Nested Table and VARRAY then it
raises a “Collection_is_Null” exception.

-- Examples of PL/SQL Collection Method DELETE without parameter.


DECLARE
TYPE my_nested_table IS TABLE OF NUMBER;
var_nt my_nested_table := my_nested_table(2,4,6,8,10,12,14,16,18,20);
BEGIN
var_nt.DELETE;
FOR i IN 1..var_nt.LAST LOOP
IF var_nt.EXISTS(i) THEN
DBMS_OUTPUT.PUT_LINE('Value at Index ['||i||'] is '|| var_nt(i));
END IF;
END LOOP;
END;
/
Calling collection procedure DELETE without any argument will delete all the elements of
the collection over which it is applied.

Example 2: Procedure call with a single parameter

DECLARE
TYPE my_nested_table IS TABLE OF NUMBER;
var_nt my_nested_table := my_nested_table(2,4,6,8,10,12,14,16,18,20);
BEGIN
DBMS_OUTPUT.PUT_LINE('After Deleted');
--Delete Specific Index
var_nt.DELETE(5);
IF var_nt.EXISTS(5) THEN
DBMS_OUTPUT.PUT_LINE('Value at Index [5] is '|| var_nt(5));
ELSE
DBMS_OUTPUT.PUT_LINE('Data is Deleted');
END IF;
END;
/

Moreover, collection procedure DELETE called by passing a valid index number


will remove the element of the specific index.

Example 3: Procedure call with two parameters.

DECLARE
TYPE my_nested_table IS
TABLE OF NUMBER;
var_nt my_nested_table := my_nested_table(2,4,6,8,10,12,14,16,18,20);
BEGIN

--Delete Range
var_nt.DELETE(2,6);
FOR i IN 1..var_nt.LAST LOOP
IF var_nt.EXISTS(i) THEN
DBMS_OUTPUT.PUT_LINE('Value at Index is’ || var_nt(i));
END IF;
END LOOP;
END;
/

Here you have to specify two Indexes and the procedure deletes the range of elements
which fall between starting-index and ending-index.
===========================================================================
Collection method: EXTEND procedure

Similar to DELETE, Collection method EXTEND is an overloaded PL/SQL procedure which is


used for appending elements to the collection.

In how many ways we can call EXTEND procedure in Oracle Database?


PL/SQL Collection Procedure is an overloaded procedure. Therefore,
it means that we call this same procedure in different ways.

--These different ways for calling Collection Procedure EXTEND are –


Extend: Extend procedure call without any argument.
Calling PL/SQL Collection procedure Extend without any argument will append a single
NULL element to the collection.

Extend (n): Extend procedure call with one argument.


Collection procedure Extend with one argument will append the number of NULL elements
that you mentioned as the argument of the procedure. But, remember that the argument
must be a valid Integer value.

Extend (n, v): Extend procedure call with two arguments.


In this case the first argument indicates the number of elements that will be appended to
the collection. Moreover the second argument is the index number. Moreover it’s value will
be copied and assigned to each of the newly appended elements of the collection.
This form of EXTEND is required for collections with “not null elements”.
--------------------------------------------------------------------------------------------------------------------------
--Q. Can we use PL/SQL Collection Method EXTEND with all three types of collections?
No, Collection Method EXTEND can only be applied to collection Nested Tables and
VARRAYs. Furthermore, EXTEND cannot be used with collection Associative Arrays.
--Q. When should we use collection method EXTEND in our code?
When you have a collection (either Nested Table or VARRAY) in your code which is not
initialized with sufficient number of elements. In that case you must first use PL/SQL
Collection Method EXTEND.

--Q. What is the requirement of PL/SQL collection Method EXTEND?


Declaring, Defining and Initializing are the three steps which we have to go through while
working with collection in Oracle Database.
But before storing the data into the index we need to create a memory slot for it.

--Q. What if we have deleted or trimmed the end of the collection?


In that case PL/SQL Collection method EXTEND will skip the deleted elements when it
assigns a new index.

--Q. What if I apply the collection method EXTEND to an uninitialized Nested table or
VARRAY?
If PL/SQL Collection Method EXTEND is applied to an uninitialized collection then it will
show a ‘COLLECTION_IS_NULL’ exception.

--Q. what if I try to EXTEND a VARRAY beyond its defined limit?


If collection method EXTEND is used with VARRAY to extend it beyond its defined limit then
you will have to face another exception which is SUBSCRIPT_BEYOND_LIMIT.

--1. PL/SQL Collection Procedure EXTEND with No Argument.


DECLARE
TYPE my_nestedTable IS TABLE OF number;
v_t my_nestedTable := my_nestedTable();
BEGIN
v_t.EXTEND;
v_t (1) := 10;
DBMS_OUTPUT.PUT_LINE ('Data at index 1 is '||v_t(1));
END;
/
o/p
Data at index 1 is 10

--2. Collection Procedure EXTEND with One Argument.


DECLARE
TYPE my_nestedTable IS TABLE OF number;
v_t my_nestedTable := my_nestedTable();
BEGIN
v_t .EXTEND(3);
v_t (1) := 10;
v_t (2) := 20;
v_t (3) := 30;
DBMS_OUTPUT.PUT_LINE ('Data at index 1 is '|| v_t (1));
DBMS_OUTPUT.PUT_LINE ('Data at index 2 is '|| v_t (2));
DBMS_OUTPUT.PUT_LINE ('Data at index 3 is '|| v_t (3));
END;
/

--3. PL/SQL Collection Procedure EXTEND with Two Arguments.

DECLARE
TYPE my_nestedTable IS TABLE OF number;
v_t my_nestedTable := my_nestedTable();
BEGIN
v_t.EXTEND;
v_t (1) := 28;
DBMS_OUTPUT.PUT_LINE ('Data at index 1 is '|| v_t (1));
v_t.EXTEND(5,1);
DBMS_OUTPUT.PUT_LINE ('Data at index 4 is '|| v_t (4));
END;
/

o/p
Data at index 1 is 28
Data at index 4 is 28

--4. Collection Procedure EXTEND (No Argument) with VARRAY

DECLARE
TYPE my_Varray IS VARRAY (5) OF NUMBER;
v_t my_Varray := my_Varray();
BEGIN
v_t.EXTEND;
v_t (1) := 10;
DBMS_OUTPUT.PUT_LINE('Data at index 1 is '|| v_t (1));
END;

===========================================================================
Collection Method: Trim Procedure

--Q. What is PL/SQL Collection Method TRIM?


Collection Method TRIM in Oracle db is an overloaded procedure using which you can
remove one or more elements from the end of a collection.

--Q. In how many ways can we call the collection method TRIM procedure in Oracle
Database?
As PL/SQL Collection Method TRIM is an Overloaded procedure thus we can call it in two
different ways.
--TRIM: Trim procedure without parameter :-
If we use TRIM procedure without any parameters then it will remove only one element
from the end of the collection.

--TRIM: Trim procedure with one parameter :-


TRIM procedure called by passing one argument will remove number of elements from the
end of the collection.

as specified by the parameter.That parameter must be a valid Integer number.


Also it should not be greater than the maximum number of elements that your collection
has.

Info: Trim procedure will raise an error if there is an attempt to trim space below zero
elements.

ORA- 06533- SUBSCRIPT_BEYOND_COUNT error

--Q. Can we use PL/SQL collection method TRIM with all the three types of collections?
Unfortunately no, we cannot. Similar to PL/SQL Collection procedure EXTEND, procedure
TRIM can only be used with collection Nested Tables and VARRAYs.
However we cannot use it with Associative Arrays.

--Q. What will happen if we use PL/SQL Collection Procedure TRIM with an Associative
array?
If collection method Trim of Oracle Database versions is used with an Associative Array then
you will get a compile time error.

--Q. What is SUBSCRIPT_BEYOND_COUNT exception?


PL/SQL Collection Method TRIM will raise SUBSCRIPT_BEYOND_COUNT exception.
If there is an attempt to trim more elements than actually exists in the collection.

--Q. Are there any other exceptions associated with PL/SQL Collection method Trim which
we should know about?
Yes, there is one more exception associated with TRIM procedure and that is
COLLECTION_IS_NULL exception.
Whenever you apply collection procedure TRIM to an uninitialized Nested Table or VARRAY
then the compiler will raise COLLECTION_IS_NULL exception.

--Q. Can we use TRIM and DELETE procedure together?


No we cannot use collections TRIM and DELETE procedure together.
Using them with each other will produce unexpected results.
Think about a scenario when you have DELETED an element situated at the end of a
VARRAY variable and thereafter apply TRIM procedure on the same.
Can you guess the number of elements that you may have removed?
You might be confused into believing that two elements have been removed however the
fact is that only one has been removed. TRIM acts upon the placeholder left by procedure
DELETE.

--examples

1. PL/SQL Collection Procedure TRIM without parameter.

DECLARE
TYPE my_nestedTable IS TABLE OF number;
v_t my_nestedTable := my_nestedTable(1,2,3,4,5);
BEGIN
v_t.TRIM;
DBMS_OUTPUT.PUT_LINE ('After TRIM procedure');
FOR i IN 1.. v_t.COUNT LOOP
DBMS_OUTPUT.PUT_LINE (v_t (i));
END LOOP;
END;
/

Output-
After TRIM procedure
1
2
3
4

2. Collection Procedure TRIM with parameter.

DECLARE
TYPE my_nestedTable IS TABLE OF number;
v_t my_nestedTable := my_nestedTable(1,2,3,4,5);
BEGIN
v_t.TRIM (3);
DBMS_OUTPUT.PUT_LINE ('After TRIM procedure');
FOR i IN 1.. v_t.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE (v_t (i));
END LOOP;
END;
/
Output-
After TRIM procedure
1
2

3. PL/SQL Collection Procedure TRIM SUBSCRIPT_BEYOND_COUNT exception.

DECLARE
TYPE my_nestedTable IS TABLE OF number;
v_t my_nestedTable := my_nestedTable(1,2,3,4,5);
BEGIN
v_t.TRIM(6);
DBMS_OUTPUT.PUT_LINE ('After TRIM procedure');
FOR i IN 1.. v_t.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE (v_t (i));
END LOOP;
END;
/
SUBSCRIPT_BEYOND_COUNT exception occurs when we pass an argument greater than the
total number of elements of the collection.

4. Collection Procedure TRIM with VARRAY.

DECLARE
TYPE inBlock_vry IS VARRAY (5) OF NUMBER;
v_t inBlock_vry := inBlock_vry(1, 2, 3, 4, 5);
BEGIN
--TRIM without parameter
v_t.TRIM;
DBMS_OUTPUT.PUT_LINE ('After TRIM procedure');
FOR i IN 1.. v_t.COUNT LOOP
DBMS_OUTPUT.PUT_LINE (v_t (i));
END LOOP;
--TRIM with Parameter
v_t.TRIM (2);
DBMS_OUTPUT.PUT_LINE ('After TRIM procedure');
FOR i IN 1.. v_t.COUNT LOOP
DBMS_OUTPUT.PUT_LINE (v_t (i));
END LOOP;
END;

Collection Exceptions
The following table provides the collection exceptions and when they are raised −
Collection Exception Raised in Situations

COLLECTION_IS_NULL You try to operate on an atomically null collection.

A subscript designates an element that was deleted, or a


NO_DATA_FOUND
nonexistent element of an associative array.

SUBSCRIPT_BEYOND_COUN A subscript exceeds the number of elements in a collection.


T

SUBSCRIPT_OUTSIDE_LIMIT A subscript is outside the allowed range.

A subscript is null or not convertible to the key type. This


exception might occur if the key is defined as
VALUE_ERROR
a PLS_INTEGER range, and the subscript is outside this
range.

=============================THE END======================================

You might also like