Host Variables and Datatypes

SQL Server uses different datatypes than does the C programming language. Embedded SQL must convert, or map, C datatypes to the appropriate SQL Server datatypes. The following Embedded SQL code fragment illustrates the mapping of three host variables that are declared as C datatypes to their corresponding SQL Server datatypes:


EXEC SQL BEGIN DECLARE SECTION;
int hostvar1 = 39;
char *hostvar2 = "telescope";
float hostvar3 = 355.95;
EXEC SQL END DECLARE SECTION;

EXEC SQL UPDATE inventory
    SET department = :hostvar1
    WHERE part_num = "4572-3";

EXEC SQL UPDATE inventory
    SET prod_descrip = :hostvar2
    WHERE part_num = "4572-3";

EXEC SQL UPDATE inventory
    SET price = :hostvar3
    WHERE part_num = "4572-3";

In the first UPDATE statement, the department column has the SQL Server smallint (integer) datatype, because the host variable hostvar1 is declared as a C int (integer) datatype. Consequently, the datatypes from C map directly to SQL Server.

In the second UPDATE statement, the prod_descrip column has the SQL Server varchar (character) datatype. The hostvar2 host variable is declared as an array of the C char (character) datatype, which maps to the SQL varchar datatype.

In the third UPDATE statement, the price column has previously been assigned the SQL Server money datatype. No datatype in C corresponds to the SQL Server money datatype. Host variables that are to be used with SQL Server money datatypes can be declared as C floating-point or character datatypes. Embedded SQL converts those host variables to and from money values.

Output host variables of datatype char are now blank padded to their full declared length which is an ANSI requirement.

Input host variables of type char that are used to input binary values must have an explicitly declared length. They cannot be pointer datatypes.

The following example is legal:


    char vBinaryIn[100];

The following example is not legal:


    char *vBinaryIn="ff00";

Be sure to carefully match the datatypes of your host variables to their corresponding use in Embedded SQL statements. For more information about mapping datatypes from the C environment to the SQL Server environment, see Appendix A, "Advanced Programming."