5. Working with Embedded SQL : Dynamic Programming : Execute a Dynamic Select Statement : How Unknown Result Column Data Types are Handled
 
Share this page                  
How Unknown Result Column Data Types are Handled
In most instances, when a dynamically defined SELECT statement is executed, the program does not know in advance the number or types of result columns. To provide this information to the program, first prepare and describe the SELECT statement. The DESCRIBE statement returns to the program the type description of the result columns of a prepared SELECT statement. After the select is described, the program must allocate (or reference) dynamically the correct number of result storage areas of the correct size and type to receive the results of the select.
If the statement is not a SELECT statement, describe returns a zero to the sqld and no sqlvar elements are used.
After the statement has been prepared and described and the result variables allocated, the program has two choices regarding the execution of the SELECT statement:
The program can associate the statement name with a cursor name, open the cursor, fetch the results into the allocated results storage area (one row at a time), and close the cursor.
The program can use EXECUTE IMMEDIATE. EXECUTE IMMEDIATE defines a select loop to process the returned rows. If the select returns only one row, it is not necessary to use a select loop.