You must declare an SQLDA structure to execute dynamically defined SQL statements. You can have the database manager include the structure definition automatically, by specifying the following statement in your source code:
EXEC SQL INCLUDE SQLDA;
You can also include the structure definition by directly coding it as shown in Figure 114.
Figure 114. SQLDA Structure (in C)
#ifndef SQLDASIZE struct sqlda { unsigned char sqldaid[8]; long sqldabc; short sqln; short sqld; struct sqlvar { short sqltype; short sqllen; unsigned char *sqldata; short *sqlind; struct sqlname { short length; unsigned char data[30]; } sqlname; } sqlvar[1]; }; #define SQLDASIZE(n) \ (sizeof(struct sqlda)+((n)-1)* \ sizeof(struct sqlvar)) #endif |
Note: | The SQLDA character array variables sqldaid and sqlname.data are not NUL-terminated. They cannot be directly used by C string manipulation functions. |
The SQLDA must not be declared within the SQL declare section.
Using the defined preprocessor function SQLDASIZE, your program can dynamically allocate an SQLDA of adequate size for use with each EXECUTE statement. For example, the code fragment below allocates an SQLDA that is adequate for five fields, and uses it in an EXECUTE of statement S3:
struct sqlda *daptr; daptr = (struct sqlda *)malloc(SQLDASIZE(5)); daptr->sqln=5; /* Add code to set the rest of values and pointers in the SQLDA */ EXEC SQL EXECUTE S3 USING DESCRIPTOR *daptr;
Note: | The variable that points to the SQLDA is not defined in an SQL declare section. Its context within an SQL statement (following INTO or USING DESCRIPTOR) is enough to identify it. |
You can use a similar technique to allocate an SQLDA for use with a DESCRIBE statement. The following program fragment illustrates the use of SQLDA with DESCRIBE for three fields and a "prepared" statement S1:
struct sqlda *daptr; EXEC SQL DECLARE C1 CURSOR FOR S1; daptr = (struct sqlda *)malloc(SQLDASIZE(3)); daptr->sqln=3; EXEC SQL DESCRIBE S1 INTO *daptr; if (daptr->sqld > daptr->sqln) --get a bigger one Set sqldata and sqlind EXEC SQL OPEN C1; EXEC SQL FETCH C1 USING DESCRIPTOR *daptr;
There is no standard C type to support packed decimal data. If you want to get data in packed decimal format, the SQLDA must be filled in with an SQLTYPE of 484 and with the appropriate values for precision and scale in SQLLEN. The C program would then have to deal with the data in its packed format.
See the DB2 Server for VSE & VM SQL Reference manual for more information on the individual fields within SQLDA.