Encoding and using the SQLLEN field in the SQLDA
The sqllen field of the sqlvar contains different values based on the sqltype of the column. For all datatypes except DECIMAL and NUMERIC, the value contained in sqllen is the actual length of the column. The sqllen for DECIMAL and NUMERIC types, contains the length, precision (identical to length), and scale. This can be decoded into length, precision, and scale variables by using the macro SQLDAGET_PREC_SCALE_LEN. The sqllen can be encoded with length, precision, and scale by using the macro SQLDAPUT_PREC_SCALE_LEN.
The following SQLTYPES require decoding or encoding of the sqllen:
SQL_TYP_DECIMAL
SQL_TYP_NUMERIC
SQL_TYP_NUMERIC_LD
SQL_TYP_NUMERIC_TR
SQL_TYP_NUMERIC_SLD
SQL_TYP_NUMERIC_STR
SQL_TYP_NUMERIC_BINARY
SQL_TYP_NUMERIC_BIN_BE
SQL_TYP_NDECIMAL
SQL_TYP_NNUMERIC
SQL_TYP_NNUMERIC_LD
SQL_TYP_NNUMERIC_TR
SQL_TYP_NNUMERIC_SLD
SQL_TYP_NNUMERIC_STR
SQL_TYP_NNUMERIC_BINARY
SQL_TYP_NNUMERIC_BIN_BE
The sqllen field in the sqlvar should remain encoded and should not be modified. If the length of the column is required, for example, when allocating storage, the decoded values sqlprec or sqllength should be used instead of sqllen.
Sample Usage
/*
Show how to decode sqllen for DECIMAL and NUMERIC type columns
The SQLDAGET_PREC_SCALE_LEN and SQLDAPUT_PREC_SCALE_LEN macros are
generated automatically by the Precompiler.
*/
int sqlprec; /* Precision */
int sqlscale; /* Scale */
int sqllength; /* Length */
if ( Sqlda->sqlvar[n].sqltype == SQL_TYP_NDECIMAL
|| SQL_TYP_NNUMERIC
|| SQL_TYP_NNUMERIC_LD
|| SQL_TYP_NNUMERIC_TR
|| SQL_TYP_NNUMERIC_SLD
|| SQL_TYP_NNUMERIC_STR
|| SQL_TYP_NNUMERIC_BINARY
|| SQL_TYP_NNUMERIC_BIN_BE)
{
SQLDAGET_PREC_SCALE_LEN(Sqlda->sqlvar[n].sqllen,
sqlprec,
sqlscale,
sqllength);
}
/*
Allocate storage using sqllength
*/
Sqlda->sqlvar[n].sqldata.host_address = calloc(1, sqllength);