Preparing the Output XSQLDA to Query Statements Without Parameters
Go Up to Method 3: Query Statements Without Parameters
Most queries return one or more rows of data, referred to as a select-list. Because the number and kind of items returned are unknown when a statement string is created, an output XSQLDA must be created to store select-list items that are returned at runtime. To prepare the XSQLDA, follow these steps:
- Declare a variable to hold the
XSQLDAneeded to store the column data for each row that will be fetched. For example, the following declaration creates anXSQLDAcalledout_sqlda:XSQLDA *out_sqlda;
- Optionally declare a variable for accessing the
XSQLVARstructure of theXSQLDA:XSQLVAR *var;
Declaring a pointer to theXSQLVARstructure is not necessary, but can simplify referencing the structure in subsequent statements. - Allocate memory for the
XSQLDAusing theXSQLDA_LENGTHmacro. The following statement allocates storage forout_sqlda:out_sqlda = (XSQLDA *)malloc(XSQLDA_LENGTH(10));
Space for tenXSQLVARstructures is allocated in this statement, enabling theXSQLDAto accommodate up to ten select-list items. - Set the
versionfield of theXSQLDAtoSQLDA_CURRENT_VERSION, and set thesqlnfield of theXSQLDAto indicate the number ofXSQLVARstructures allocated:out_sqlda->version = SQLDA_CURRENT_VERSION; out_sqlda->sqln = 10;