SQL Data Type Macro Constants
Go Up to Understanding the XSQLDA
InterBase defines a set of macro constants to represent SQL data types and NULL status information in an XSQLVAR. An application should use these macro constants to specify the data type of parameters and to determine the data types of select-list items in a SQL statement. The following table lists each SQL data type, its corresponding macro constant expression, C data type or InterBase typedef, and whether or not the sqlind field is used to indicate a parameter or variable that contains NULL or unknown data:
| SQL data type | Macro expression | C data type or typedef |
|
|---|---|---|---|
|
|
|
|
No |
|
|
|
|
Yes |
|
|
|
|
No |
|
|
|
|
Yes |
|
|
|
signed short |
No |
|
|
|
signed short |
Yes |
|
|
|
char[] |
No |
|
|
|
char[] |
Yes |
|
|
|
|
No |
|
|
|
|
Yes |
|
|
|
int, |
No |
|
|
|
int, |
Yes |
|
|
|
double |
No |
|
|
|
double |
Yes |
|
|
|
long |
No |
|
|
|
|
Yes |
|
|
|
float |
No |
|
|
|
float |
Yes |
|
|
|
int, |
No |
|
|
|
int, |
Yes |
|
|
|
short |
No |
|
|
|
short |
Yes |
|
|
|
|
No |
|
|
|
|
Yes |
|
|
|
|
No |
|
|
|
|
Yes |
|
|
|
First 2 bytes: short containing the length of the character string; remaining bytes: char[] |
No |
|
|
|
First 2 bytes: short containing the length of the character string; remaining bytes: char[] |
Yes |
DECIMAL and NUMERIC data types are stored internally as SMALLINT, INTEGER, DOUBLE PRECISION, or 64-bit integer data types. To specify the correct macro expression to provide for a DECIMAL or NUMERIC column, use isql to examine the column definition in the table to see how InterBase is storing column data, then choose a corresponding macro expression.
The data type information for a parameter or select-list item is contained in the sqltype field of the XSQLVAR structure. The value contained in sqltype provides two pieces of information:
- The data type of the parameter or select-list item.
- Whether
sqlindis used to indicateNULLvalues. Ifsqlindis used, its value specifies whether the parameter or select-list item isNULL(–1), or notNULL(0).
For example, if sqltype equals SQL_TEXT, the parameter or select-list item is a CHAR that does not use sqlind to check for a NULL value (because, in theory, NULL values are not allowed for it). If sqltype equals SQL_TEXT + 1, then sqlind can be checked to see if the parameter or select-list item is NULL.
The C language expression,
sqltype & 1, provides a useful test of whether a parameter or select-list item can contain a NULL. The expression evaluates to 0 if the parameter or select-list item cannot contain a NULL, and 1 if the parameter or select-list item can contain a NULL. The following code fragment demonstrates how to use the expression:if (sqltype & 1 == 0) {
/* parameter or select-list item that CANNOT contain a NULL */
}
else {
/* parameter or select-list item CAN contain a NULL */
}
By default, both isc_dsql_prepare() and isc_dsql_describe() return a macro expression of type + 1, so sqlind should always be examined for NULL values with these statements.