Oracle Collection API โ€“ Methods and Exception Handling in PL/SQL

Oracle provides a robust Collection API that enables developers to efficiently interact with and manipulate collection types such as VARRAYs, nested tables, and associative arrays. Initially introduced in Oracle 8i and significantly enhanced in later versions, the Collection API offers a set of built-in functions and procedures that simplify operations such as accessing, modifying, and traversing collections.

Although earlier versions of PL/SQL allowed limited collection manipulation, Oracleโ€™s introduction of associative arrays necessitated mastery of the Collection API, particularly the FIRST, LAST, NEXT, and PRIOR methodsโ€”essential for navigating collections indexed by strings.

Overview of Oracle Collection API Methods

The Collection API includes both functions (which return values) and procedures (which perform actions without returning values). Below is a categorized summary of commonly used methods.

Collection Methods โ€“ Summary Table

MethodDescriptionReturn TypeApplicable To
COUNTReturns the current number of elements in the collection. May be less than LIMIT in varrays.PLS_INTEGERAll
DELETE(n)Removes the element at index n.NoneAll
DELETE(n, m)Removes a range of elements from index n to m, inclusive.NoneAll
EXISTS(n)Returns TRUE if an element exists at index n, otherwise FALSE. Does not raise exceptions on NULL.BOOLEANAll
EXTENDAdds a new uninitialized element to the end of the collection.NoneVARRAY, Nested Table
EXTEND(n)Appends n new uninitialized elements.NoneVARRAY, Nested Table
EXTEND(n, i)Appends n new elements, each a copy of the i-th element.NoneVARRAY, Nested Table
FIRSTReturns the lowest index value currently in use.Index typeAll
LASTReturns the highest index value currently in use.Index typeAll
LIMITReturns the maximum number of elements allowed in a varray.PLS_INTEGERVARRAY
NEXT(n)Returns the next higher index after n; returns NULL if none exists.Index typeAll
PRIOR(n)Returns the next lower index before n; returns NULL if none exists.Index typeAll
TRIMRemoves the last element from the collection.NoneVARRAY, Nested Table
TRIM(n)Removes the last n elements from the collection.NoneVARRAY, Nested Table

Standard Exceptions in Collection Handling

Proper use of collection methods requires awareness of common exceptions that may arise during runtime. Oracle defines the following standard exceptions specific to collections:

ExceptionRaised When
COLLECTION_IS_NULLAttempting to access or modify a collection that has not been initialized.
NO_DATA_FOUNDAccessing a deleted or non-existent subscript (e.g., in associative arrays with string keys).
SUBSCRIPT_BEYOND_COUNTReferencing an index greater than the collection’s current element count (varrays, nested tables).
SUBSCRIPT_OUTSIDE_LIMITUsing an index outside the allowable range defined by LIMIT (for varrays only).
VALUE_ERRORAssigning a value incompatible with PLS_INTEGER for numeric indexes.

Practical Notes

  • The EXISTS(n) method is the only one that safely operates on uninitialized collections without raising an exception.

  • Methods like NEXT, PRIOR, FIRST, and LAST are critical when working with sparse or string-indexed associative arrays.

  • While EXTEND, TRIM, and DELETE are procedures, others like COUNT, LIMIT, FIRST, LAST, NEXT, and PRIOR are functions and return values for evaluation.

๐Ÿ“˜ AmantPoint Exclusive Learning Series
ยฉ 2025 AmantPoint. All rights reserved. Educational content for developers and learners.

Scroll to Top