Collection Built-Ins (Methods)

PL/SQL offers a number of built-infunctions and procedures, known as collection methods, that let you obtain information about and modify the contents of collections. Here is the complete list of these programs:

Method (function or procedure) Description
COUNT function Returns the current number of elements in a collection.
DELETE procedure Removes one or more elements from the "middle" of a nested table. Reduces COUNT if the element is not already DELETEd. Can be used for VARRAYS, but only to delete the entire contents of the collection.
EXISTS function Returns TRUE or FALSE to indicate whether the specified element exists.
EXTEND procedure Increases the number of elements in a collection. Increases COUNT. Does not apply to associative arrays.
FIRST, LASTfunctions Return the smallest (FIRST) and largest (LAST) subscripts in use.
LIMIT function Returns the maximum number of allowed elements in a VARRAY.
PRIOR, NEXTfunctions Return the subscript immediately before (PRIOR) or after (NEXT) a specified subscript. You should always use PRIOR and NEXT to traverse a collection, especially if you are working with sparse (or potentially sparse) collections.
TRIM procedure Removes collection elements at the "end" of the collection. Reduces COUNT if elements are not DELETEd.

These programs are referred to as "methods" because the syntax for using the collection built-ins is different from the normal syntax used to call procedures and functions. Collection methods employ a "member method" syntax that's common in object-oriented languages such as C++.

To give you a feel for member-method syntax, consider the LAST function. It returns the greatest index value in use in the associative array. Using standard function syntax, you might expect to call LAST as follows:

IF LAST (company_table) > 10 THEN ... /* Invalid syntax */

In other words, you would pass the associative array as an argument. In contrast, by using the member-method syntax, the LAST function is a method that "belongs to" the object—in this case, the associative array. So the correct syntax for using LAST is:

IF company_table.LAST > 10 THEN ... /* Correct syntax */

The general syntax for calling these associative array built-ins is either of the following:

· An operation that takes no arguments:

table_name.operation

· An operation that takes a row index for an argument:

table_name.operation(index_number [, index_number])

The following statement, for example, returns TRUE if the 15th row of the company_tab associative array is defined:

company_tab.EXISTS(15)

The collection methods are not available from within SQL; they can be used only in PL/SQL programs.

Наши рекомендации