Inserting more than one element

An insert cursor that includes an INSERT statement with the collection-derived table clause allows you to insert many elements into a collection variable.

About this task

To insert elements, follow these steps:

Procedure

  1. Create a client collection variable in your program.
  2. Declare the insert cursor for the collection variable with the DECLARE statement and open the cursor with the OPEN statement.
  3. Put the element or elements into the collection variable with the PUT statement and the FROM clause.
  4. Close the insert cursor with the CLOSE statement, and if you no longer need the cursor, free it with the FREE statement.
  5. After the collection variable contains all the elements, you then use the UPDATE statement or the INSERT statement on a table name to save the contents of the collection variable in a collection column (SET, MULTISET, or LIST).

    For more information, see Operate on a collection column.

Results

Tip: Instead of an insert cursor, you can use an INSERT statement to insert elements one at a time into a collection variable. However, an insert cursor is more efficient for large insertions.

For more information, see Insert one element.

For sample code that inserts several elements into a collection variable, see Insertion of many elements into a collection host variable.