4.10 Collections

Table of Contents Previous Next


4 Stored Procedure Language : 4.10 Collections

A collection is a set of ordered data items with the same data type. Generally, the data item is a scalar field, but may also be a user-defined type such as a record type or an object type (see Section 8 for information about object types) as long as the structure and the data types that comprise each field of the user-defined type are the same for each element in the set. Each particular data item in the set is referenced by using subscript notation within a pair of parentheses.
Note: Multilevel collections (that is, where the data item of a collection is another collection) are not supported.
The most commonly known type of collection is an array. In Advanced Server, the supported collection types are associative arrays (formerly called index-by-tables in Oracle), nested tables, and varrays.
A collection of the desired type must be defined. This can be done in the declaration section of an SPL program, which results in a local type that is accessible only within that program. For nested table and varray types this can also be done using the CREATE TYPE command, which creates a persistent, standalone type that can be referenced by any SPL program in the database.
Variables of the collection type are declared. The collection associated with the declared variable is said to be uninitialized at this point if there is no value assignment made as part of the variable declaration.
Uninitialized collections of nested tables and varrays are null. A null collection does not yet exist. Generally, a COLLECTION_IS_NULL exception is thrown if a collection method is invoked on a null collection.
An associative array is a type of collection that associates a unique key with a value. The key does not have to be numeric, but can be character data as well.
An associative array type must be defined after which array variables can be declared of that array type. Data manipulation occurs using the array variable.
The key can be any negative integer, positive integer, or zero if INDEX BY BINARY_INTEGER or PLS_INTEGER is specified.
The key can be character data if INDEX BY VARCHAR2 is specified.
The TYPE IS TABLE OF ... INDEX BY statement is used to define an associative array type.
TYPE assoctype IS TABLE OF { datatype | rectype | objtype }
INDEX BY { BINARY_INTEGER | PLS_INTEGER | VARCHAR2(n) };
assoctype is an identifier assigned to the array type. datatype is a scalar data type such as VARCHAR2 or NUMBER. rectype is a previously defined record type. objtype is a previously defined object type. n is the maximum length of a character key.
In order to make use of the array, a variable must be declared with that array type. The following is the syntax for declaring an array variable.
array assoctype
array is an identifier assigned to the associative array. assoctype is the identifier of a previously defined array type.
array(n)[.field ]
array is the identifier of a previously declared array. n is the key value, type-compatible with the data type given in the INDEX BY clause. If the array type of array is defined from a record type or object type, then [.field ] must reference an individual field within the record type or attribute within the object type from which the array type is defined. Alternatively, the entire record can be referenced by omitting [.field ].
The following example reads the first ten employee names from the emp table, stores them in an array, then displays the results from the array.
The emp%ROWTYPE attribute could be used to define emp_arr_typ instead of using the emp_rec_typ record type as shown in the following.
A nested table is a type of collection that associates a positive integer with a value. A nested table has the following characteristics:
A nested table type must be defined after which nested table variables can be declared of that nested table type. Data manipulation occurs using the nested table variable, or simply, “table” for short.
When a nested table variable is declared, the nested table initially does not exist (it is a null collection). The null table must be initialized with a constructor. You can also initialize the table by using an assignment statement where the right-hand side of the assignment is an initialized table of the same type. Note: Initialization of a nested table is mandatory in Oracle, but optional in SPL.
The constructor establishes the number of elements in the table. The EXTEND method adds additional elements to the table. See Section 4.11 for information on collection methods. Note: Usage of the constructor to establish the number of elements in the table and usage of the EXTEND method to add additional elements to the table are mandatory in Oracle, but optional in SPL.
The TYPE IS TABLE statement is used to define a nested table type within the declaration section of an SPL program.
TYPE tbltype IS TABLE OF { datatype | rectype | objtype };
tbltype is an identifier assigned to the nested table type. datatype is a scalar data type such as VARCHAR2 or NUMBER. rectype is a previously defined record type. objtype is a previously defined object type.
In order to make use of the table, a variable must be declared of that nested table type. The following is the syntax for declaring a table variable.
table tbltype
table is an identifier assigned to the nested table. tbltype is the identifier of a previously defined nested table type.
tbltype ([ { expr1 | NULL } [, { expr2 | NULL } ] [, ...] ])
tbltype is the identifier of the nested table type’s constructor, which has the same name as the nested table type. expr1, expr2, … are expressions that are type-compatible with the element type of the table. If NULL is specified, the corresponding element is set to null. If the parameter list is empty, then an empty nested table is returned, which means there are no elements in the table. If the table is defined from an object type, then exprn must return an object of that object type. The object can be the return value of a function or the object type’s constructor, or the object can be an element of another nested table of the same type.
If a collection method other than EXISTS is applied to an uninitialized nested table, a COLLECTION_IS_NULL exception is thrown. See Section 4.11 for information on collection methods.
table(n)[.element ]
table is the identifier of a previously declared table. n is a positive integer. If the table type of table is defined from a record type or object type, then [.element ] must reference an individual field within the record type or attribute within the object type from which the nested table type is defined. Alternatively, the entire record or object can be referenced by omitting [.element ].
The following example reads the first ten employee names from the emp table, stores them in a nested table, then displays the results from the table. The SPL code is written to assume that the number of employees to be returned is not known beforehand.
Note the creation of an empty table with the constructor emp_tbl_typ() as the first statement in the executable section of the anonymous block. The EXTEND collection method is then used to add an element to the table for each employee returned from the result set. See Section 4.11.4 for information on EXTEND.
The following example shows how a nested table of an object type can be used. See Section 8 for information about object types and objects. First, an object type is created with attributes for the department name and location.
The following anonymous block defines a nested table type whose element consists of the dept_obj_typ object type. A nested table variable is declared, initialized, and then populated from the dept table. Finally, the elements from the nested table are displayed.
Note: The parameters comprising the nested table’s constructor, dept_tbl_typ, are calls to the object type’s constructor dept_obj_typ.
4.10.3 Varrays
A varray or variable-size array is a type of collection that associates a positive integer with a value. In many respects, it is similar to a nested table.
A varray type must be defined along with a maximum size limit. After the varray type is defined, varray variables can be declared of that varray type. Data manipulation occurs using the varray variable, or simply, “varray” for short. The number of elements in the varray cannot exceed the maximum size limit established in the varray type definition.
When a varray variable is declared, the varray initially does not exist (it is a null collection). The null varray must be initialized with a constructor. You can also initialize the varray by using an assignment statement where the right-hand side of the assignment is an initialized varray of the same type.
The constructor establishes the number of elements in the varray, which must not exceed the maximum size limit. The EXTEND method can add additional elements to the varray up to the maximum size limit. See Section 4.11 for information on collection methods.
The TYPE IS VARRAY statement is used to define a varray type within the declaration section of an SPL program.
TYPE varraytype IS { VARRAY | VARYING ARRAY }(maxsize)
OF { datatype | objtype };
varraytype is an identifier assigned to the varray type. datatype is a scalar data type such as VARCHAR2 or NUMBER. maxsize is the maximum number of elements permitted in varrays of that type. objtype is a previously defined object type.
Note: The CREATE TYPE command can be used to define a varray type that is available to all SPL programs in the database. See the
CREATE TYPE command.
In order to make use of the varray, a variable must be declared of that varray type. The following is the syntax for declaring a varray variable.
varray varraytype
varray is an identifier assigned to the varray. varraytype is the identifier of a previously defined varray type.
varraytype ([ { expr1 | NULL } [, { expr2 | NULL } ]
varraytype is the identifier of the varray type’s constructor, which has the same name as the varray type. expr1, expr2, … are expressions that are type-compatible with the element type of the varray. If NULL is specified, the corresponding element is set to null. If the parameter list is empty, then an empty varray is returned, which means there are no elements in the varray. If the varray is defined from an object type, then exprn must return an object of that object type. The object can be the return value of a function or the return value of the object type’s constructor. The object can also be an element of another varray of the same varray type.
If a collection method other than EXISTS is applied to an uninitialized varray, a COLLECTION_IS_NULL exception is thrown. See Section 4.11 for information on collection methods.
varray(n)[.element ]
varray is the identifier of a previously declared varray. n is a positive integer. If the varray type of varray is defined from an object type, then [.element ] must reference an attribute within the object type from which the varray type is defined. Alternatively, the entire object can be referenced by omitting [.element ].

4 Stored Procedure Language : 4.10 Collections

Table of Contents Previous Next