Oracle PL/SQL - PL SQL Data Type Collection Constructors

Introduction

Collection Constructors applies only to varrays and nested tables.

Associative arrays do not have constructors.

A collection constructor is a function with the same name as a collection type.

It returns a collection of that type. 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.

The following code calls a constructor twice.

Demo

SQL>
SQL> DECLARE-- from   w w w.  j  a  v a2  s .  c om
  2    TYPE StringList IS VARRAY(4) OF VARCHAR2(15);
  3    team StringList := StringList();  -- initialize to empty
  4
  5    PROCEDURE print_team (heading VARCHAR2)
  6    IS
  7    BEGIN
  8      DBMS_OUTPUT.PUT_LINE(heading);
  9
 10      IF team.COUNT = 0 THEN
 11        DBMS_OUTPUT.PUT_LINE('Empty');
 12      ELSE
 13        FOR i IN 1..4 LOOP
 14          DBMS_OUTPUT.PUT_LINE(i || '.' || team(i));
 15        END LOOP;
 16      END IF;
 17    END;
 18
 19  BEGIN
 20    print_team('Team:');
 21    team := StringList('A', 'B', 'C', 'D');
 22    print_team('Team:');
 23  END;
 24  /
Team:
Empty
Team:
1.A
2.B
3.C
4.D

PL/SQL procedure successfully completed.

SQL>

Related Topic