A varray (short for variable array) is an ordered collection of elements of the same data type. Each element is identified by an index that corresponds to its position in the array.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
The size of a varray is limited. When you create a varray, you must specify its maximum length.
The syntax for creating a varray is as follows:
TYPE varray_name IS VARRAY( size ) OF element_type [ NOT NULL ] ;
In this syntax, varray_name is the name of the varray data type. size is a positive integer that specifies the maximum number of elements that the array can hold. Each element has a data type of element_type. By default, elements can be null. If you want to disallow null values, use the NOT NULL constraint.
Example: Create a varray
obclient> DECLARE
TYPE players IS VARRAY(5) OF VARCHAR2(20);
team PLAYERS := players('Andrew', 'Barton', 'Conrad', 'Dick','Edward');
PROCEDURE print_team (heading VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(heading);
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE('---');
END;
BEGIN
print_team('First Team:');
team(2) := 'Albert';
team(5) := 'George';
print_team('Second Team:');
team := players('Charles', 'Carl', 'James', 'Gary','Brian');
print_team('Third Team:');
END;
/
Query OK, 0 rows affected
First Team:
1.Andrew
2.Barton
3.Conrad
4.Dick
5.Edward
---
Second Team:
1.Andrew
2.Albert
3.Conrad
4.Dick
5.George
---
Third Team:
1.Charles
2.Carl
3.James
4.Gary
5.Brian
---
Varrays are suitable for the following scenarios:
The maximum number of elements is known.
Elements are typically accessed in sequence.
Since varrays require all elements to be stored or retrieved at once, they are not suitable for scenarios with a large number of elements.
