Writing a program or SQL procedure to receive the result sets from a stored procedure

You can write a program to receive results sets from a stored procedure for either a fixed number of result sets, for which you know the contents, or a variable number of result sets, for which you do not know the contents.

Returning a known number of result sets is simpler to write, but if you write the code to handle a varying number of result sets you do not need to make major modifications to your program if the stored procedure changes.

The basic steps for receiving result sets are as follows:

  1. Declare a locator variable for each result set that will be returned.

    If you do not know how many result sets will be returned, declare enough result set locators for the maximum number of result sets that might be returned.

  2. Call the stored procedure and check the SQL return code.

    If the SQLCODE from the CALL statement is +466, the stored procedure has returned result sets.

  3. Determine how many result sets the stored procedure is returning.

    If you already know how many result sets the stored procedure returns, you can skip this step.

    Use the SQL statement DESCRIBE PROCEDURE to determine the number of result sets. DESCRIBE PROCEDURE places information about the result sets in an SQLDA or SQL descriptor.

    For an SQL descriptor, when the DESCRIBE PROCEDURE statement completes, the following values can be retrieved:

    • DB2_RESULT_SETS_COUNT contains the number of result sets returned by the stored procedure.
    • One descriptor area item is returned for each result set:
      • DB2_CURSOR_NAME contains the name of the cursor used by the stored procedure to return the result set.
      • The DB2_RESULT_SET_ROWS contains the estimated number of rows in the result set. A value of -1 indicates that no estimate of the number of rows in the result set is available.
      • DB2_RESULT_SET_LOCATOR contains the value of the result set locator associated with the result set.

    For an SQLDA, make the SQLDA large enough to hold the maximum number of result sets that the stored procedure might return. When the DESCRIBE PROCEDURE statement completes, the fields in the SQLDA contain the following values:

    • SQLD contains the number of result sets returned by the stored procedure.
    • Each SQLVAR entry gives information about a result set. In an SQLVAR entry:
      • The SQLNAME field contains the name of the cursor used by the stored procedure to return the result set.
      • The SQLIND field contains the estimated number of rows in the result set. A value of -1 indicates that no estimate of the number of rows in the result set is available.
      • The SQLDATA field contains the value of the result set locator, which is the address of the result set.
  4. Link result set locators to result sets.

    You can use the SQL statement ASSOCIATE LOCATORS to link result set locators to result sets. The ASSOCIATE LOCATORS statement assigns values to the result set locator variables. If you specify more locators than the number of result sets returned, the extra locators will be ignored.

    If you executed the DESCRIBE PROCEDURE statement previously, the result set locator values can be retrieved from the DB2_RESULT_SET_LOCATOR in the SQL descriptor or from the SQLDATA fields of the SQLDA. You can copy the values from these fields to the result set locator variables manually, or you can execute the ASSOCIATE LOCATORS statement to do it for you.

    The stored procedure name that you specify in an ASSOCIATE LOCATORS or DESCRIBE PROCEDURE statement must be a procedure name that has already been used in the CALL statement that returns the result sets.

  5. Allocate cursors for fetching rows from the result sets.

    Use the SQL statement ALLOCATE CURSOR to link each result set with a cursor. Execute one ALLOCATE CURSOR statement for each result set. The cursor names can be different from the cursor names in the stored procedure.

  6. Determine the contents of the result sets.

    If you already know the format of the result set, you can skip this step.

    Use the SQL statement DESCRIBE CURSOR to determine the format of a result set and put this information in an SQL descriptor or an SQLDA. For each result set, you need an SQLDA big enough to hold descriptions of all columns in the result set.

    You can use DESCRIBE CURSOR only for cursors for which you executed ALLOCATE CURSOR previously.

    After you execute DESCRIBE CURSOR, if the cursor for the result set is declared WITH HOLD, for an SQL descriptor DB2_CURSOR_HOLD can be checked. For an SQLDA the high-order bit of the eighth byte of field SQLDAID in the SQLDA is set to 1.

  7. Fetch rows from the result sets into host variables by using the cursors that you allocated with the ALLOCATE CURSOR statements.

    If you executed the DESCRIBE CURSOR statement, perform these steps before you fetch the rows:

    1. Allocate storage for host variables and indicator variables. Use the contents of the SQL descriptor or SQLDA from the DESCRIBE CURSOR statement to determine how much storage you need for each host variable.
    2. Put the address of the storage for each host variable in the appropriate SQLDATA field of the SQLDA.
    3. Put the address of the storage for each indicator variable in the appropriate SQLIND field of the SQLDA.

    Fetching rows from a result set is the same as fetching rows from a table.