A variable-size array (or varray) is a collection of ordered elements. All the elements have the same data type and the index of each element corresponds to the position of the element in the array.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL mode.
The size of a varray is limited. When you create a varray, you must specify its maximum length.
Syntax for creating a varray:
TYPE varray_name IS VARRAY( size ) OF element_type [ NOT NULL ] ;
varray_name specifies the name of the varray. The value of size is a positive integer indicating the maximum number of elements in the array. element_type specifies the data type of all elements in the array. By default, null values are allowed for elements unless a NOT NULL constraint is added.
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
---
A varray is applicable to a scenario with the following conditions:
The maximum number of elements is known.
The elements are usually accessed in order.
A varray must store or retrieve all elements at the same time, and therefore is inapplicable to scenarios with a large number of elements.