Using datatypes

In Embedded SQL, you can use the C datatypes char, int, float, double, and void. You can use the keywords const and volatile, though not with structures. You can use the keywords unsigned, long, and short. You can use storage class specifiers: auto, extern, register, and static.

NoteDo not use long int when building 64-bit applications.

exec sql begin declare section;
     register int frequently_used_host_variable;
     extern char 
     shared_string_host_variable[STRING_SIZE];
     /*
     ** The const restriction is not enforced by
     ** the precompiler; only the compiler makes use
     ** of it.
     */
     const float 
     input_only_host_variable = 3.1415926;
     /*
     ** Be careful.  You can declare unsigned
     ** integers, but if you select a negative
     ** number into one, you will get an incorrect
     ** result and no error message.
     */
     unsigned long int unsigned_host_variable;
 exec sql end declare section;

You can declare pointers in the declare section, but you cannot use a pointer as a host variable in an Embedded SQL statement.

exec sql begin declare section;
 int number;
 /*
 ** It's convenient to declare this here,
 ** but we won't be using it as a host variable.
 */
 int *next_number;
 exec sql end declare section;

You can use the following Sybase datatypes:

CS_BINARY, CS_BIT, CS_BIGINT, CS_BOOL, CS_CHAR, CS_DATE, CS_DATETIME, CS_DATETIME4, CS_DECIMAL, CS_FLOAT, CS_REAL, CS_IMAGE, CS_INT, CS_MONEY, CS_MONEY4, CS_NUMERIC, CS_RETCODE, CS_SMALLINT, CS_TEXT, CS_TIME CS_TINYINT, CS_UBIGINT, CS_UINT, CS_UNICHAR, CS_UNITEXT, CS_USMALLINT, CS_VOID, CS_XML.

CS_CHAR is treated differently from char; CS_CHAR is null-terminated but not blank-padded; char is null-terminated and blank-padded to the length of the array.

/*
 ** Your #define for the array size doesn't 
 ** have to be in the declare section, 
 ** though it would be legal if it were.
 */
 #define MAX_NAME 40;
 
 exec sql begin declare section;
     CS_MONEY salary;
     CS_CHAR print_this[MAX_NAME];
     char print_this_also[MAX_NAME];
 exec sql end declare section;
 
 exec sql select salary into :salary from salaries 
     where employee_ID = '01234';
 /* 
 ** The CS_MONEY type is not directly printable. 
 ** Here's an easy way to do a conversion.
 */
 exec sql select :salary into :print_this;
 
 /*
 ** This will not be blank-padded.
 */
 printf("Salary for employee 01234 is %s.\n", 
     print_this);
 
 /*
 ** This will be blank-padded.
 */
 exec sql select :salary into :print_this_also;
 printf("Salary for employee 01234 is %s.\n", 
     print_this_also);