PL provides a data structure similar to arrays, which stores a fixed-size ordered collection of elements of the same type for storing ordered data sets.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL mode.
An array consists of consecutive memory locations. The least significant bit corresponds to the first element, and the most significant bit corresponds to the last element.
As shown in the following example, you can create an array type by using the command line:
CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) of <element_type>;
As shown in the following example, you can dynamically create an array type in a PL block:
TYPE varray_type_name IS VARRAY(n) of <element_type>;
In this example, varray_type_name is the name of the array type, n specifies the number of elements, and element_type specifies the type of elements.
obclient> DECLARE
TYPE alpha IS TABLE OF VARCHAR2(1);
l alpha := alpha('A', 'B', 'C', 'D');
BEGIN
FOR i IN l.FIRST .. l.LAST LOOP
DBMS_OUTPUT.PUT_LINE(l(i));
END LOOP;
DBMS_OUTPUT.PUT_LINE('-');
END;
/
Query OK, 0 rows affected
A
B
C
D
-
The basic structure of a nested table is similar to that of an array. However, the two differ in the following aspects:
An array must specify the length when it is defined, but a nested table does not need to. A nested table can be dynamically extended.
An array is continuous, and the subscripts of the array change when elements are deleted. A nested table is continuous when it is initialized, but the subscripts do not change when elements are deleted. The deleted elements still occupy the positions.