Previous Topic: Executing a Bulk FetchNext Topic: Executing a Bulk Insert


Executing a Bulk Select

A bulk select is a SELECT statement that retrieves multiple rows from the database into a host variable array:

  1. Declare a host variable array
  2. Issue the SELECT statement with a BULK clause, as in this example:
    EXEC SQL
       SELECT DEPT_ID,
              DEPT_NAME,
              DIV-CODE,
              DEPT_HEAD_ID
          BULK :BULK-DEPARTMENT
          FROM DEPARTMENT
    END-EXEC.
    

Checking the Status of a Bulk Select

A successful bulk select returns 100 to SQLCODE. A value of 100 will be returned if there are fewer result rows than entries in the bulk array or if the number of result rows is the same as the number of entries. If the array is too small for the result table, the statement returns a cardinality violation error.

The following table shows the possible combinations of SQLCODE and SQLCNRP values on a bulk select:

Result of bulk select

SQLCODE value

SQLCNRP value

No rows are returned

100

0

At least one row is returned but fewer rows than the maximum allowed

100

Greater than 0 and less than or equal to the maximum allowed

The number of rows returned exceeds the maximum allowed

Less than 0

Equal to the maximum allowed

Advantage of a Bulk Select

A bulk select retrieves a set of rows using fewer resources than a series of single-row SELECT statements to retrieve the same rows.

Bulk Select Considerations

A bulk select:

A bulk select is appropriate only when selecting from a table with a number of rows that you consider fixed, such as a table of the 50 states and their mailing codes.

If the size of the host variable array may be too small for the result table, you should declare a cursor for the SELECT statement and use a bulk fetch.