Host Variables and Data Types

Embedded SQL for C and SQL Server

Embedded SQL for C and SQL Server

Host Variables and Data Types

Microsoft® SQL Server™ 2000 uses different data types than the C programming language. ESQL/C must map C data types to the appropriate SQL Server data types. The following Embedded SQL code fragment shows the mapping of three host variables, declared as C data types, to their corresponding SQL Server data types:

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) data type because the host variable hostvar1 is declared as a C int (integer) data type. Consequently, the data types from C map directly to SQL Server.

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

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

Note  Output host variables of data type char are padded with blanks to their full declared length, which is an SQL-92 requirement.

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

The following example is correct:

char vBinaryIn[100];

The following example is incorrect:

char *vBinaryIn="ff00";

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