Using varrays v16
A varray or variable-size array is a type of collection that associates a positive integer with a value. In many respects, it's similar to a nested table.
Varray overview
A varray has the following characteristics:
- You must define a varray type with a maximum size limit. After you define the varray type, you can declare varray variables of that varray type. Data manipulation occurs using the varray variable, also known simply as a varray. The number of elements in the varray can't exceed the maximum size limit set in the varray type definition.
- When you declare a varray variable, the varray at first is a null collection. You must initialize the null varray 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 key is a positive integer.
- The constructor sets the number of elements in the varray, which must not exceed the maximum size limit. The
EXTEND
method can add elements to the varray up to the maximum size limit. For details, see Collection methods. - Unlike a nested table, a varray cannot be sparse. There are no gaps when assigning values to keys.
- An attempt to reference a varray element beyond its initialized or extended size but within the maximum size limit results in a
SUBSCRIPT_BEYOND_COUNT
exception. - An attempt to reference a varray element beyond the maximum size limit or extend a varray beyond the maximum size limit results in a
SUBSCRIPT_OUTSIDE_LIMIT
exception.
Defining a varray type
The TYPE IS VARRAY
statement is used to define a varray type in the declaration section of an SPL program:
Where:
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.
You can use the CREATE TYPE
command to define a varray type that's available to all SPL programs in the database. To make use of the varray, you must declare a variable of that varray type. The following is the syntax for declaring a varray variable:
Where:
varray
is an identifier assigned to the varray.
varraytype
is the identifier of a previously defined varray type.
Initializing a varray
Initialize a varray using the varray type’s constructor: