Nested Tables v11
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 key is a positive integer.
- The constructor establishes the number of elements in the table. The
EXTEND
method adds additional elements to the table. See Collection Methods for information on collection methods. Note: Usage of the constructor to establish the number of elements in the table and usage of theEXTEND
method to add additional elements to the table are mandatory in Oracle, but optional in SPL. - The table can be sparse - there may be gaps in the assignment of values to keys.
- An attempt to reference a table element beyond its initialized or extended size will result in a
SUBSCRIPT_BEYOND_COUNT
exception.
The TYPE IS TABLE
statement is used to define a nested table type within the declaration section of an SPL program.
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.
Note
You can use the CREATE TYPE
command to define a nested table type that is available to all SPL programs in the database. See the Database Compatibility for Oracle Developers SQL Guide for more information about the CREATE TYPE
command.
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
is an identifier assigned to the nested table. tbltype
is the identifier of a previously defined nested table type.
A nested table is initialized using the nested table type’s constructor.
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 Collection Methods for information on collection methods.
The following is an example of a constructor for a nested table:
An element of the table is referenced using the following syntax.
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 is an example of a nested table where it is known that there will be four elements.
The above example produces the following output:
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 Extend for information on EXTEND
.
The following is the output.
The following example shows how a nested table of an object type can be used. 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.
The parameters comprising the nested table’s constructor, dept_tbl_typ
, are calls to the object type’s constructor dept_obj_typ.
The following is the output from the anonymous block: