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

CharacteristicNested tableVARRAY
Maximum sizeNo limitFixed at type definition
Declaration keywordTYPE ... IS TABLE OFTYPE ... IS VARRAY(n) OF

Syntax

TYPE type_name IS TABLE OF datatype [NULL | NOT NULL];
ParameterDescription
type_nameThe name of the nested table type.
datatypeThe data type of each element.
NULL | NOT NULLOptional. 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
DO