Collection Constructors
A collection constructor (constructor) is a system-defined function with the same name as a collection type, which returns a collection of that type.
Note:
This topic applies only to varrays and nested tables. In this topic, collection means varray or nested table. Associative arrays use qualified expressions and aggregates (see Qualified Expressions Overview).
The syntax of a constructor invocation is:
collection_type ( [ value [, value ]... ] )
If the parameter list is empty, the constructor returns an empty collection. Otherwise, the constructor returns a collection that contains the specified values. For semantic details, see "collection_constructor".
You can assign the returned collection to a collection variable (of the same type) in the variable declaration and in the executable part of a block.
Example 6-7 Initializing Collection (Varray) Variable to Empty
This example invokes a constructor twice: to initialize the varray variable team
to empty in its declaration, and to give it new values in the executable part of the block. The procedure print_team
shows the initial and final values of team
. To determine when team
is empty, print_team
uses the collection method COUNT
, described in "Collection Methods". (For an example of a procedure that prints a varray that might be null, see Example 6-30.)
Live SQL:
You can view and run this example on Oracle Live SQL at Initializing Collection (Varray) Variable to Empty
DECLARE
TYPE Foursome IS VARRAY(4) OF VARCHAR2(15);
team Foursome := Foursome(); -- initialize to empty
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..4 LOOP
DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
END LOOP;
END IF;
DBMS_OUTPUT.PUT_LINE('---');
END;
BEGIN
print_team('Team:');
team := Foursome('John', 'Mary', 'Alberto', 'Juanita');
print_team('Team:');
END;
/
Result:
Team:
Empty
---
Team:
1.John
2.Mary
3.Alberto
4.Juanita
---