Nested tables
Updated at:
A nested table is a collection type that stores an ordered set of elements with no maximum size limit. Unlike a variable-length array (VARRAY), a nested table can grow dynamically.
Nested tables vs. VARRAYs
| Characteristic | Nested table | VARRAY |
|---|---|---|
| Maximum size | No limit | Fixed at type definition |
| Declaration keyword | TYPE ... IS TABLE OF | TYPE ... IS VARRAY(n) OF |
Syntax
TYPE type_name IS TABLE OF datatype [NULL | NOT NULL];| Parameter | Description |
|---|---|
type_name | The name of the nested table type. |
datatype | The data type of each element. |
NULL | NOT NULL | Optional. When NOT NULL is specified, NULL elements are not allowed. If omitted or NULL is specified, NULL elements are permitted. |
Declare and use a nested table
Declare the type and variable
TYPE nt_type IS TABLE OF INT;
nt nt_type := nt_type(); -- Initialize as an empty table.Add elements and assign values
nt.extend(1); -- Allocate one new element slot at the end (index 1 becomes accessible).
nt(1) := 1; -- Assign a value to index 1.Read elements
RAISE NOTICE '%', nt(1); -- Print the value at index 1.Example
The following example declares a nested table of integers, initializes it as empty, adds one element, assigns a value, and prints it.
DECLARE
TYPE nt_type IS TABLE OF INT;
nt nt_type := nt_type(); -- Initialize as an empty table.
BEGIN
nt.extend(1); -- Add one element slot (index 1 becomes accessible).
nt(1) := 1; -- Assign a value to index 1.
RAISE NOTICE '%', nt(1); -- Print the value at index 1.
END;Expected output:
NOTICE: 1
DOIs this page helpful?