PL/SQL - Arrays
In this chapter, we will discuss arrays in PL/SQL. The PL/SQL programming
language provides a data structure called the VARRAY, which can store a fixed-
size sequential collection of elements of the same type. A varray is used to store an
ordered collection of data, however it is often better to think of an array as a
collection of variables of the same type.
All varrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
An array is a part of collection type data and it stands for variable-size arrays. We
will study other collection types in a later chapter 'PL/SQL Collections'.
Each element in a varray has an index associated with it. It also has a maximum
size that can be changed dynamically.
Creating a Varray Type
A varray type is created with the CREATE TYPE statement. You must specify the
maximum size and the type of elements stored in the varray.
The basic syntax for creating a VARRAY type at the schema level is −
CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) of
<element_type>
Where,
varray_type_name is a valid attribute name,
n is the number of elements (maximum) in the varray,
element_type is the data type of the elements of the array.
Maximum size of a varray can be changed using the ALTER TYPE statement.
For example,
CREATE Or REPLACE TYPE namearray AS VARRAY(3) OF VARCHAR2(10);
/
Type created.
The basic syntax for creating a VARRAY type within a PL/SQL block is −
TYPE varray_type_name IS VARRAY(n) of <element_type>
For example −
TYPE namearray IS VARRAY(5) OF VARCHAR2(10);
Type grades IS VARRAY(5) OF INTEGER;
In this chapter, we will discuss arrays in PL/SQL. The PL/SQL programming
language provides a data structure called the VARRAY, which can store a fixed-
size sequential collection of elements of the same type. A varray is used to store an
ordered collection of data, however it is often better to think of an array as a
collection of variables of the same type.
All varrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
An array is a part of collection type data and it stands for variable-size arrays. We
will study other collection types in a later chapter 'PL/SQL Collections'.
Each element in a varray has an index associated with it. It also has a maximum
size that can be changed dynamically.
Creating a Varray Type
A varray type is created with the CREATE TYPE statement. You must specify the
maximum size and the type of elements stored in the varray.
The basic syntax for creating a VARRAY type at the schema level is −
CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) of
<element_type>
Where,
varray_type_name is a valid attribute name,
n is the number of elements (maximum) in the varray,
element_type is the data type of the elements of the array.
Maximum size of a varray can be changed using the ALTER TYPE statement.
For example,
CREATE Or REPLACE TYPE namearray AS VARRAY(3) OF VARCHAR2(10);
/
Type created.
The basic syntax for creating a VARRAY type within a PL/SQL block is −
TYPE varray_type_name IS VARRAY(n) of <element_type>
For example −
TYPE namearray IS VARRAY(5) OF VARCHAR2(10);
Type grades IS VARRAY(5) OF INTEGER;