Collection Constructors

Use a collection constructor to specify values for a collection column.
(1)
Collection Constructors

1 SET
1 MULTISET
1 LIST
2  {?  + , %Expression1 }
Notes:

You can use collection constructors in the WHERE clause of the SELECT statement and the VALUES clause of the INSERT statement. You can also pass collection constructors to UDRs.

This table differentiates the types of collections that you can construct.
Keyword Description
SET Indicates a collection of elements with the following qualities:
  • The collection must contain unique values.
  • Elements have no specific order associated with them.
MULTISET Indicates a collection of elements with the following qualities:
  • The collection can contain duplicate values.
  • Elements have no specific order associated with them.
LIST Indicates a collection of elements with the following qualities:
  • The collection can contain duplicate values.
  • Elements have ordered positions.

The element type of the collection can be any built-in or extended data type. You can use any kind of expression with a collection constructor, including literals, functions, and variables.

When you use a collection constructor with a list of expressions, the database server evaluates each expression to its equivalent literal form and uses the literal values to construct the collection.

You specify an empty collection with a set of empty braces ( { } ).

Elements of a collection cannot be NULL. If a collection element evaluates to a NULL value, the database server returns an error.

The element type of each expression must all be exactly the same data type. To accomplish this, cast the entire collection constructor expression to a collection type, or cast individual element expressions to the same type. If the database server cannot determine that the collection type and the element types are homogeneous, then the collection constructor returns an error. In the case of host variables, this determination is made at bind time when the client declares the element type of the host variable.

An exception to this restriction can occur when some elements of a collection are VARCHAR data types but others are longer than 255 bytes. Here the collection constructor can assign a CHAR(n) type to all elements, for n the length in bytes of the longest element. (But see Collection Data Types for an example based on this exception, where the user avoids fixed-length CHAR elements by an explicit cast to the LVARCHAR data type.)