The most complex way to use dynamic SQL for SELECT statements is when you do not know (when you write the application program) the number and data types of the columns in the SELECT's result table. This requires the use of varying-list SELECT statements.
As the example below shows, a program that uses varying-list SELECT statements must do the following:
EXEC SQL DECLARE CURSOR1 CURSOR FOR STMT;
PREPARE USERSTMT INTO SMLSQLDA FROM :SQLSTRING;
IF SMLSQLDA.SQLD = 0 THEN
the statement was not a select-statement--ERROR
ENDIF
The application program should now allocate an SQLDA of the size indicated by SMLSQLDA.SQLD. If we call this full-size SQLDA LRGSQLDA, to get the description of the result table filled in, the application program should then execute a DESCRIBE statement, using LRGSQLDA.
EXEC SQL DESCRIBE STMT INTO LRGSQLDA;
Now we have an SQLDA that describes (in its SQLVAR section) all of the columns of the result table of the select-statement in SQLSTRING. Examine the SQLDA to allocate storage for a result row of the select-statement. Set the addresses in the SQLVAR entries to point to memory allocated for each entry.
EXEC SQL OPEN CURSOR1;
Or, if there were parameter markers in the select-statement:
EXEC SQL OPEN CURSOR1 USING DESCRIPTOR SQLDAPARA;
EXEC SQL FETCH CURSOR1 USING DESCRIPTOR LRGSQLDA;
EXEC SQL CLOSE CURSOR1;
|
Copyright © 2014 CA.
All rights reserved.
|
|