Was this helpful?
How to Declare an SQLDA Variable
Once the SQLDA definition has been included (or hard coded), the program can declare an SQLDA variable. You must declare this variable outside a declare section, because the preprocessor does not understand the special meaning of the SQLDA. When you use the variable, the preprocessor accepts any object name and assumes that the variable points at a legal SQLDA. The actual SQLDA area must be either dynamically allocated or statically declared and pointed at by the variable.
Dynamic Allocation of an SQLDA
In order to dynamically allocate an SQLDA, you must call an allocation routine (such as the C calloc function) and cast the result as a pointer to an SQLDA. The allocation call must include one header (IISQDA_HEAD_SIZE) and any number of variables (N * IISQDA_VAR_SIZE). For example, the following program fragment dynamically allocates an SQLDA with number variables, and points the variable sqlda at the allocated memory. As soon as the SQLDA is allocated, the sqln field is set to record how many array elements were allocated:
exec sql include sqlda;
IISQLDA *sqlda; /* Pointer to an SQLDA */
** 'number' has been assigned a positive number.
** Note that the result of the allocation call, calloc,
** is cast to be a pointer to an SQLDA.
** The calloc routine is passed 2 parameters
** (number of objects, and size of a single object).
sqlda = (IISQLDA *)calloc(1, IISQDA_HEAD_SIZE +
       (number * IISQDA_VAR_SIZE));
if (sqlda == (IISQLDA *)0) /* Memory error */
    /* Print error and exit */
    err_exit("Failure allocating %d SQLDA elements\n",
sqlda->sqln = number; /* Set the size */
exec sql describe s1 into :sqlda;
If you change the above allocation call to:
sqlda = (IISQLDA *)calloc(1, sizeof(IISQLDA));
then IISQ_MAX_COLS elements is allocated. This number of elements is the current maximum for data retrievals. In this case, the sqln field should be set to IISQ_MAX_COLS.
Static Declaration of an SQLDA
As previously mentioned, you can statically declare an SQLDA as well as dynamically allocate one. The C file that is included when issuing the include sqlda statement specifies some C macros that help a program tailor the size of the statically declared SQLDA. In fact, the IISQLDA type definition is derived from that macro.
If a program requires a statically declared SQLDA with the same number of variables as the IISQLDA type, then it can use code like the following:
exec sql include sqlda;
IISQLDA _sqlda;
 IISQLDA *sqlda = &_sqlda;
sqlda->sqln = IISQ_MAX_COLS; /* Set the size */
exec sql describe s1 into :sqlda;
Even though a pointer to an SQLDA is required when describing or executing a statement, it is also acceptable to use the syntax:
exec sql describe s1 into :&_sqlda;
You must confirm that the SQLDA object being used is a pointer to a valid SQLDA.
If a program requires a statically declared SQLDA with a different number of variables (not IISQ_MAX_COLS), it can use the macro IISQLDA_TYPE. This macro is described in more detail in the eqsqlda.h include file that is generated by include sqlda. (If you are not familiar with C macros then skip the following discussion). The syntax of IISQLDA_TYPE is:
IISQLDA_TYPE(tag_name, sqlda_name, number_of_sqlvars);
IISQLDA_TYPE is a macro that declares object sqlda_name (a type definition or a variable) of an SQLDA-like structure with tag tag_name, and with num_of_sqlvars SQLDA variables. For example, the following declaration declares a local SQLDA, called sqlda10 with 10 variables. The variable sqlda10 is not a pointer.
IISQLDA_TYPE(da10_, sqlda10, 10);
The following example declares a static SQLDA with 32 variables, and a pointer to the SQLDA.
Example: Static declaration of an SQLDA
static IISQLDA_TYPE(da32_, sqlda32, 32);
 struct da32_ *da32_ptr = &sqlda32;
Last modified date: 06/10/2024