A collection constructor is a system-defined function with the same name as the collection type. It returns a collection of the specified type.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL mode.
Note
Constructors are applicable only to varrays and nested tables. Associative arrays do not have constructors. In this topic, a collection refers to a varray or a nested table.
The syntax for calling a constructor is as follows:
collection_type ( [ value [, value ]... ] )
In this syntax, collection_type is the name of a previously declared nested table or varray type (not an associative array type). value is a valid value of the collection_type element. If collection_type is a varray type, the number of values cannot exceed the maximum value. If collection_type is a nested table type, there is no maximum value.
If the parameter list is empty, the constructor returns an empty collection. Otherwise, the constructor returns a collection containing the specified values.
You can assign the returned collection to a collection variable of the same type in a variable declaration or in the executable part of a block.
Example: Initialize a collection variable to an empty value.
obclient> DECLARE
TYPE players IS VARRAY(5) OF VARCHAR2(20);
team PLAYERS := players(); -- Initialize to an empty value
PROCEDURE print_team (heading VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(heading);
IF team.COUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE('Empty');
ELSE
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
END LOOP;
END IF;
DBMS_OUTPUT.PUT_LINE('---');
END;
BEGIN
print_team('Team Members:');
team := players('Charles', 'Carl', 'James', 'Gary','Brian');
print_team('Team Members:');
END;
/
Query OK, 0 rows affected
Team Members:
Empty
---
Team Members:
1.Charles
2.Carl
3.James
4.Gary
5.Brian
---
In the preceding example, the constructor is called twice: first to initialize the varray variable team to an empty value, and then in the executable part of the block to assign it a new value. The print_team stored procedure displays the initial and final values of team. The print_team procedure uses the collection method COUNT to determine whether team is empty.