Assigning Values to Collection Variables
You can assign a value to a collection variable in these ways:
-
Invoke a constructor to create a collection and assign it to the collection variable.
-
Use the assignment statement to assign it the value of another existing collection variable.
-
Pass it to a subprogram as an
OUT
orIN
OUT
parameter, and then assign the value inside the subprogram. -
Use a qualified expression to assign values to an associative array (see Example 6-11).
To assign a value to a scalar element of a collection variable, reference the element as collection_variable_name
(
index
)
and assign it a value.
Topics
See Also:
-
"Assignment Statement" syntax diagram
-
"Assigning Values to Variables" for instructions on how to assign a value to a scalar element of a collection variable
Data Type Compatibility
You can assign a collection to a collection variable only if they have the same data type. Having the same element type is not enough.
Example 6-14 Data Type Compatibility for Collection Assignment
In this example, VARRAY
types triplet
and trio
have the same element type, VARCHAR(15)
. Collection variables group1
and group2
have the same data type, triplet
, but collection variable group3
has the data type trio
. The assignment of group1
to group2
succeeds, but the assignment of group1
to group3
fails.
Live SQL:
You can view and run this example on Oracle Live SQL at Data Type Compatibility for Collection Assignment
DECLARE TYPE triplet IS VARRAY(3) OF VARCHAR2(15); TYPE trio IS VARRAY(3) OF VARCHAR2(15); group1 triplet := triplet('Jones', 'Wong', 'Marceau'); group2 triplet; group3 trio; BEGIN group2 := group1; -- succeeds group3 := group1; -- fails END; /
Result:
ORA-06550: line 10, column 13: PLS-00382: expression is of wrong type
Assigning Null Values to Varray or Nested Table Variables
To a varray or nested table variable, you can assign the value NULL
or a null collection of the same data type. Either assignment makes the variable null.
Example 6-15 initializes the nested table variable dept_names
to a non-null value; assigns a null collection to it, making it null; and re-initializes it to a different non-null value.
Example 6-15 Assigning Null Value to Nested Table Variable
Live SQL:
You can view and run this example on Oracle Live SQL at Assigning Null Value to Nested Table Variable
DECLARE
TYPE dnames_tab IS TABLE OF VARCHAR2(30);
dept_names dnames_tab := dnames_tab(
'Shipping','Sales','Finance','Payroll'); -- Initialized to non-null value
empty_set dnames_tab; -- Not initialized, therefore null
PROCEDURE print_dept_names_status IS
BEGIN
IF dept_names IS NULL THEN
DBMS_OUTPUT.PUT_LINE('dept_names is null.');
ELSE
DBMS_OUTPUT.PUT_LINE('dept_names is not null.');
END IF;
END print_dept_names_status;
BEGIN
print_dept_names_status;
dept_names := empty_set; -- Assign null collection to dept_names.
print_dept_names_status;
dept_names := dnames_tab (
'Shipping','Sales','Finance','Payroll'); -- Re-initialize dept_names
print_dept_names_status;
END;
/
Result:
dept_names is not null.
dept_names is null.
dept_names is not null.
Assigning Set Operation Results to Nested Table Variables
To a nested table variable, you can assign the result of a SQL MULTISET
operation or SQL SET
function invocation.
The SQL MULTISET
operators combine two nested tables into a single nested table. The elements of the two nested tables must have comparable data types. For information about the MULTISET
operators, see Oracle Database SQL Language Reference.
The SQL SET
function takes a nested table argument and returns a nested table of the same data type whose elements are distinct (the function eliminates duplicate elements). For information about the SET
function, see Oracle Database SQL Language Reference.
Example 6-16 Assigning Set Operation Results to Nested Table Variable
This example assigns the results of several MULTISET
operations and one SET
function invocation of the nested table variable answer
, using the procedure print_nested_table
to print answer
after each assignment. The procedure uses the collection methods FIRST
and LAST
, described in "Collection Methods".
Live SQL:
You can view and run this example on Oracle Live SQL at Assigning Set Operation Results to Nested Table Variable
DECLARE TYPE nested_typ IS TABLE OF NUMBER; nt1 nested_typ := nested_typ(1,2,3); nt2 nested_typ := nested_typ(3,2,1); nt3 nested_typ := nested_typ(2,3,1,3); nt4 nested_typ := nested_typ(1,2,4); answer nested_typ; PROCEDURE print_nested_table (nt nested_typ) IS output VARCHAR2(128); BEGIN IF nt IS NULL THEN DBMS_OUTPUT.PUT_LINE('Result: null set'); ELSIF nt.COUNT = 0 THEN DBMS_OUTPUT.PUT_LINE('Result: empty set'); ELSE FOR i IN nt.FIRST .. nt.LAST LOOP -- For first to last element output := output || nt(i) || ' '; END LOOP; DBMS_OUTPUT.PUT_LINE('Result: ' || output); END IF; END print_nested_table; BEGIN answer := nt1 MULTISET UNION nt4; print_nested_table(answer); answer := nt1 MULTISET UNION nt3; print_nested_table(answer); answer := nt1 MULTISET UNION DISTINCT nt3; print_nested_table(answer); answer := nt2 MULTISET INTERSECT nt3; print_nested_table(answer); answer := nt2 MULTISET INTERSECT DISTINCT nt3; print_nested_table(answer); answer := SET(nt3); print_nested_table(answer); answer := nt3 MULTISET EXCEPT nt2; print_nested_table(answer); answer := nt3 MULTISET EXCEPT DISTINCT nt2; print_nested_table(answer); END; /
Result:
Result: 1 2 3 1 2 4 Result: 1 2 3 2 3 1 3 Result: 1 2 3 Result: 3 2 1 Result: 3 2 1 Result: 2 3 1 Result: 3 Result: empty set