Use SQLSetDescField to set decimal or numeric datatypes

You see an error if you do not specify the precision and scale using the ODBC API method – SQLSetDescField – when retrieving data from numeric or decimal columns in a table.

Workaround: Specify the SQLSetDescField with value types SQL_DESC_PRECISION and SQL_DESC_SCALE.

This code illustrates how to retrieve a numeric column from a table specifying a precision and scale.

/*
Insert values
Execute select statement
*/

/*
Fetch Values
*/

#define ROW_SIZE 10
SQLRETURN sr;
SQL_NUMERIC_STRUCT      g[ROW_SIZE];
SQLLEN  gLen[ROW_SIZE];
SQLINTEGER intVal[ROW_SIZE];
SQLLEN intLen[ROW_SIZE];;

sr = SQLBindCol(hStmt, 1, SQL_C_LONG, intVal, sizeof(SQLINTEGER), intLen);
sr = SQLBindCol(hStmt, 2, SQL_C_NUMERIC, g, sizeof(SQL_NUMERIC_STRUCT), gLen);

SQLHDESC hdesc = NULL;
SQLGetStmtAttr(hStmt, SQL_ATTR_APP_ROW_DESC, &hdesc, 0, NULL);
SQLSetDescField(hdesc, 2, SQL_DESC_PRECISION, SQLPOINTER) 5, 0);
SQLSetDescField(hdesc, 2, SQL_DESC_SCALE, (SQLPOINTER) 2, 0);
SQLUSMALLINT rowStatus[ROW_SIZE];

sr = SQLSetStmtAttr(hStmt, SQL_ATTR_ROW_STATUS_PTR, rowStatus, 0);
for (short i = 0; i < ROW_SIZE; i++)
{
	 memset(&g[i], '\0', sizeof(SQL_NUMERIC_STRUCT));
	 memset(g[i].val, 0, 16);
}
sr = SQLFetch(StatementHandle);

See the Microsoft ODBC API Reference.