Host Variables in C
C host variables used in SQL statements must be declared within the SQL BEGIN DECLARE SECTION and END DECLARE SECTION statements. CONNX Embedded SQL allows the use of single host variables and host variable structures.
Host Variable Declaration
C structures are named sets of C single host variables and must conform to the ANSI Standard (X3.159-1989) for C. The use of C structures within SQL statements is a CONNX Embedded SQL extension and not part of the ANSI SQL Standard. It is not possible to use a union of host variables. The use of the enum type is also not possible.
Binary Data Type
There is no intrinsic binary data type in the C host language. In order to retrieve and supply binary data using host variables, the intrinsic C data type of character must be mapped to the CONNX Embedded SQL data type of binary. A pseudo type has been introduced using a CONNX Embedded SQL macro. Therefore, host variables, which will be used for binary data type transportation, must be declared as SAGBINARY. The CONNX Embedded SQL will then associate such variables with the type binary. Such variables can only be used as binary host variables. The type declaration is a C macro, provided by the CONNX Embedded SQL. A SAGBINARY variable is declared as follows:
EXEC SQL BEGIN DECLARE SECTION;
SAGBINARY ( hv_name, x );
EXEC SQL END DECLARE SECTION;
where hv_name is the name of the host variable and x is the length of the variable in bits.
The macro is resolved as follows:
char hv_name[y];
where y is the required length in bytes.
Binary host variables are not subject to byte swapping, nor are they subject to any ASCII/ EBCDIC conversion. No string terminator is to be provided for binary host variables. Therefore, the direct binary contents of a host variable will be received by CONNX Embedded SQL, with each element of the character array representing a full 8 bytes.
The pre-compiler must run prior to the C macro processor. No corresponding functionality is supported for the C language when using dynamic SQL and the SQLDA.
Syntax
Within embedded SQL statements, the C naming qualification rules for structure and array elements are as defined in the ANSI standard (X3.159-1989) for C. There can be any number of SQL BEGIN DECLARE SECTION statements.
Ambiguous References and Multiple Declarations
A declaration that appears more than once with the same identifier is called a multiple declaration. If a host variable refers to a multiple declaration, and the different declarations are of different types, an error occurs. Otherwise the host variable is accepted.
Data Type Conversion
The following table shows the conversion of C data types to SQL data types:
C Data Types | SQL Data Types |
char (array) | CHARACTER |
long (signed or unsigned) | INTEGER |
int (signed or unsigned) | INTEGER |
float | REAL |
double | DOUBLE PRECISION |
short (signed or unsigned) | SMALLINT |
long long | BIGINT |
For more details on SQL data types and their usage in SQL statements refer to the section on Common Elements in this online help file.
The following table shows the conversion of SQL data types to C data types:
SQL Data Types | C Data Types |
CHAR (more than one character) | char (array) |
BINARY | SAGBINARY (char array) |
INTEGER | long |
SMALLINT | short |
REAL | float |
DOUBLE PRECISION | double |
FLOAT | float |
DECIMAL | float, double or long |
NUMERIC | float, double or long |
BIGINT | long long |