Preparing the SELECT Statement to Read Data from an Array for Execution
From InterBase
Go Up to Reading Data from an Array
After an XSQLDA
is created for holding the column data for each selected row, the query statement string can be prepared for execution. Follow these steps:
- Declare and initialize a SQL statement handle, then allocate it with
isc_dsql_allocate_statement()
:isc_stmt_handle stmt; /* Declare a statement handle. */ stmt = NULL; /* Set handle to NULL before allocation. */ isc_dsql_allocate_statement(status_vector, &db_handle, &stmt);
- Ready the statement string for execution with
isc_dsql_prepare()
. This checks the string (sel_str
) for syntax errors, parses it into a format that can be efficiently executed, and sets the statement handle (stmt
) to refer to this parsed format. The statement handle is used in a later call toisc_dsql_execute
(). Ifisc_dsql_prepare()
is passed a pointer to the outputXSQLDA
, as in the following example, it will fill in most fields of theXSQLDA
and all itsXSQLVAR
substructures with information such as the data type, length, and name of the corresponding columns in the statement. A sample call toisc_dsql_prepare()
is:isc_dsql_prepare( status_vector, &trans, /* Set by previous isc_start_transaction() call. */ &stmt, /* Statement handle set by this function call. */ 0, /* Specifies statement string is null-terminated. */ sel_str, /* Statement string. */ 1, /* da_version number. */ out_sqlda); /* XSQLDA for storing column data. */
- Set up an
XSQLVAR
structure for each column. Setting up anXSQLVAR
structure involves the following steps: For columns whose types are known at compile time:- Specify the data type of the column (if it was not set by
isc_dsql_prepare()
, as previously described). - Point the
sqldata
field of theXSQLVAR
to an appropriate local variable.
- Coerce the data type of the item (optional); for example, from
SQL_VARYING
toSQL_TEXT
. - Dynamically allocate local storage for the data pointed to by the
sqldata
field of theXSQLVAR
.
NULL
value indicator for the parameter.- Data retrieval for array (and Blob) columns is different from other types of columns, so the
XSQLVAR
fields must be set differently. For non-array (and non-Blob) columns,isc_dsql_prepare()
sets eachXSQLVAR
sqltype
field to the appropriate field type, and the data retrieved when a data of a select list row is fetched is placed into thesqldata
space allocated for the column. For array columns, the type is set toSQL_ARRAY
(orSQL_ARRAY
+ 1 if the array column is allowed to beNULL
). InterBase stores the internal array identifier (array ID), not the array data, in thesqldata
space when a row’s data is fetched, so you must pointsqldata
to an area the size of an array ID. To see how to retrieve the actual array or array slice data once you have an array ID, see Reading and Processing the Array Data. - The following code example illustrates the assignments for array and non-array columns whose types are known at compile time. For more information about DSQL and the
XSQLDA
, and working with columns whose types are unknown until run time, see Working with Dynamic SQL.
ISC_QUAD array_id = 0L; char dept_no[6]; short flag0, flag1; out_sqlda->sqlvar[0].sqldata = (char *) dept_no; out_sqlda->sqlvar[0].sqltype = SQL_TEXT + 1; out_sqlda->sqlvar[0].sqlind = &flag0; out_sqlda->sqlvar[1].sqldata = (char *) &array_id; out_sqlda->sqlvar[1].sqltype = SQL_ARRAY + 1; out_sqlda->sqlvar[1].sqlind = &flag1;
- Specify the data type of the column (if it was not set by