In dynamic SQL, varying-list SELECT statements are ones for which
the number and format of result columns to be returned are not predictable;
that is, you do not know how many variables you need, or what the data types
are.
Therefore, you cannot define host variables in advance to accommodate
the result columns returned.
Note: In REXX, steps 5.b,
6, and 7 are not applicable. REXX only supports SQL descriptors defined using
the SQLDA structure; it does not support allocated SQL descriptors.
If your application accepts varying-list SELECT statements, your
program has to:
- Place the input SQL statement into a host variable.
- Issue a PREPARE statement to validate the dynamic SQL statement and put
it into a form that can be run. If DLYPRP (*YES) is specified on the CRTSQLxxx
command, the preparation is delayed until the first time the statement is
used in an EXECUTE or DESCRIBE statement, unless the USING clause is specified
on the PREPARE statement.
- Declare a cursor for the statement name.
- Open the cursor (declared in step 3) that includes the name of the dynamic
SELECT statement.
- For an allocated SQL descriptor, run an ALLOCATE DESCRIPTOR
statement to define the descriptor you intend to use.
- Issue a DESCRIBE statement to request information from SQL about the type
and size of each column of the result table.
Notes: - You can also code the PREPARE statement with an INTO clause to perform
the functions of PREPARE and DESCRIBE with a single statement.
If using an SQLDA and the SQLDA is not large enough to contain
column descriptions for each retrieved column, the program must determine
how much space is needed, get storage for that amount of space, build a new
SQLDA, and reissue the DESCRIBE statement.
If using
an allocated SQL descriptor and the descriptor is not large enough, deallocate
the descriptor, allocate it with a larger number of entries, and reissue the
DESCRIBE statement.
- For an SQLDA descriptor, allocate the amount of storage needed
to contain a row of retrieved data.
- For an SQLDA descriptor, put storage addresses into the SQLDA
to tell SQL where to put each item of retrieved data.
- FETCH a row.
- Process the data returned in the SQL descriptor.
- Handle any SQL return codes that might result.
- When end of data occurs, close the cursor.
- For an allocated SQL descriptor, run a DEALLOCATE
DESCRIPTOR statement to delete the descriptor.