Coercing Data Types (Embedded SQL Guide)
Go Up to Understanding the XSQLDA (Embedded SQL Guide)
Contents
Sometimes when processing DSQL input parameters and select-list items, it is desirable or necessary to translate one data type to another. This process is referred to as data type coercion. For example, data type coercion is often used when parameters or select-list items are of type VARCHAR
. The first two bytes of VARCHAR
data contain string length information, while the remainder of the data is the string to process. By coercing the data from SQL_VARYING
to SQL_TEXT
, data processing can be simplified.
Coercion can only be from one compatible data type to another. For example, SQL_VARYING
to SQL_TEXT
, or SQL_SHORT
to SQL_LONG
.
Coercing Character Data Types (Embedded SQL Guide)
To coerce SQL_VARYING
data types to SQL_TEXT
data types, change the sqltype
field in the parameter’s or select-list item’s XSQLVAR
structure to the desired SQL macro data type constant. For example, the following statement assumes that var
is a pointer to an XSQLVAR
structure, and that it contains a SQL_VARYING
data type to convert to SQL_TEXT
:
var->sqltype = SQL_TEXT;
After coercing a character data type, provide proper storage space for it. The XSQLVAR
field, sqllen
, contains information about the size of the uncoerced data. Set the XSQLVAR
sqldata
field to the address of the data.
Coercing Numeric Data Types (Embedded SQL Guide)
To coerce one numeric data type to another, change the sqltype
field in the parameter’s or select-list item’s XSQLVAR
structure to the desired SQL macro data type constant. For example, the following statement assumes that var
is a pointer to an XSQLVAR
structure, and that it contains a SQL_SHORT
data type to convert to SQL_LONG
:
var->sqltype = SQL_LONG;
Do not coerce a larger data type to a smaller one. Data can be lost in such a translation.
Setting a NULL Indicator (Embedded SQL Guide)
If a parameter or select-list item can contain a NULL
value, the sqlind
field is used to indicate its NULL
status. Appropriate storage space must be allocated for sqlind
before values can be stored there.
On insertion, set sqlind
to –1 to indicate that NULL
values are legal. Otherwise set sqlind
to 0.
On selection, a sqlind
of –1 indicates a field contains a NULL
value. Other values indicate a field contains non-NULL
data.