Contents Index Declaring host variables Host variable usage

ASA Programming Guide
  Embedded SQL Programming
    Using host variables

C host variable types


Only a limited number of C data types are supported as host variables. Also, certain host variable types do not have a corresponding C type.

Macros defined in the sqlca.h header file can be used to declare host variables of the following types: VARCHAR, FIXCHAR, BINARY, PACKED DECIMAL, LONG VARCHAR, LONG BINARY, or SQLDATETIME structure. They are used as follows:

EXEC SQL BEGIN DECLARE SECTION;
DECL_VARCHAR( 10 ) v_varchar;
DECL_FIXCHAR( 10 ) v_fixchar;
DECL_LONGVARCHAR( 32678 ) v_longvarchar;
DECL_BINARY( 4000 ) v_binary;
DECL_LONGBINARY( 128000 ) v_longbinary;
DECL_DECIMAL( 10, 2 ) v_packed_decimal;
DECL_DATETIME v_datetime;
EXEC SQL END DECLARE SECTION;

The preprocessor recognizes these macros within a declaration section and treats the variable as the appropriate type.

The following table lists the C variable types that are allowed for host variables and their corresponding embedded SQL interface data types.

C Data Type Embedded SQL Interface Type Description
short i;
short int i;
unsigned short int i;
DT_SMALLINT 16-bit signed integer
long l;
long int l;
unsigned long int l;
DT_INT 32-bit signed integer
float f;
DT_FLOAT 4-byte floating point
double d;
DT_DOUBLE 8-byte floating point
DECL_DECIMAL(p,s)
DT_DECIMAL(p,s) Packed decimal
char a; /*n=1*/ 
DECL_FIXCHAR(n) a;
DECL_FIXCHAR a[n];
DT_FIXCHAR(n) Fixed length character string blank padded.
char a[n]; /*n>=1*/
DT_STRING(n) NULL-terminated string. The string is blank-padded if the database is initialized with blank-padded strings.
char *a;
DT_STRING(32767) NULL-terminated string
DECL_VARCHAR(n) a;
DT_VARCHAR(n) Varying length character string with 2-byte length field. Not blank padded
DECL_BINARY(n) a;
DT_BINARY(n) Varying length binary data with 2-byte length field
DECL_DATETIME a;
DT_TIMESTAMP_STRUCT SQLDATETIME structure
DECL_LONGVARCHAR( n ) a;
DT_LONGVARCHAR Varying length long character string with three 4-byte length fields. Not blank padded or NULL terminated.
DECL_LONGBINARY( n ) a;
DT_LONGBINARY Varying length long binary data with three 4-byte length fields. Not blank padded.
Pointers to char 

A host variable declared as a pointer to char (char *a) is considered by the database interface to be 32 767 bytes long. Any host variable of type pointer to char used to retrieve information from the database must point to a buffer large enough to hold any value that could possibly come back from the database.

This is potentially quite dangerous because somebody could change the definition of the column in the database to be larger than it was when the program was written. This could cause random memory corruption problems. If you are using a 16-bit compiler, requiring 32 767 bytes could make the program stack overflow. It is better to use a declared array, even as a parameter to a function, where it is passed as a pointer to char. This lets the PREPARE statements know the size of the array.

Scope of host variables 

A standard host-variable declaration section can appear anywhere that C variables can normally be declared. This includes the parameter declaration section of a C function. The C variables have their normal scope (available within the block in which they are defined). However, since the SQL preprocessor does not scan C code, it does not respect C blocks.

As far as the SQL preprocessor is concerned, host variables are global; two host variables cannot have the same name.


Contents Index Declaring host variables Host variable usage