I am facing a problem with the retrieval of decimal values
from an MS-SQLSERVER database.
I have a table with a column of type decimal with a precision
of 28 and scale 0, I am using the following C program
with the SQL SERVER DB-Library 6.0 interface.
Once the program is executed , It appears that the numbers with
around 16 significant(non decimal) digits are
retrieved/converted correctly.
I am not sure whether the truncation of the numbers occurs
during the retrieval time or it is done by the dbconvert
routine, I have not been able to gather much information
about the limitations of the dbconvert routine from the SQL
SERVER BOOKS ONLINE HELP.
Could someone suggest a method to overcome this problem ?.
Thanks in anticipation
Armstrong Monteiro
TABLE NAME :- newtab
Column name & type :- decimaldata(28,0)
Database Column Values as retrieved through Microsoft ISQL/W
-------------------------
999999999999999999999
6666666666
66666666666666666666
11111111111111111111
99999999999999999
9999999999999999
8888888888888888888888888
77777777777777777
RESULTs of the DB-LIBRARY PROGRAM
----------------------------------
1000000000000000000000.0
6666666666.0
66666666666666664000.0
11111111111111111000.0
100000000000000000.0
9999999999999998.0
8888888888888888700000000.0
77777777777777776.0
------------------------------------------------
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>
// Forward declarations of the error handler and message handler.
int err_handler(PDBPROCESS, INT, INT, INT, LPCSTR, LPCSTR);
int msg_handler(PDBPROCESS, DBINT, INT, INT, LPCSTR, LPCSTR,
LPCSTR, DBUSMALLINT);
main()
{
PDBPROCESS dbproc; // The connection with SQL Server.
PLOGINREC login; // The login information.
DBCHAR converted[100];
int null_val,ret;
DBDECIMAL decval;
// Install user-supplied error- and message-handling functions.
dberrhandle (err_handler);
dbmsghandle (msg_handler);
// Initialize DB-Library.
dbinit ();
// Get a LOGINREC.
login = dblogin ();
DBSETLUSER (login, "sa");
DBSETLPWD (login, "sa123");
// Get a DBPROCESS structure for communication with SQL Server.
dbproc = dbopen (login, "armstrong");
dbuse(dbproc,"datatypes");
dbcmd (dbproc, "select decimaldata from newtab where decimaldata is
not null");
dbcmd (dbproc, " ");
// Send the command to SQL Server and start execution.
dbsqlexec (dbproc);
// Process the results.
if (dbresults (dbproc) == SUCCEED)
{
dbbind (dbproc, 1, SRCDECIMALBIND, 0,(BYTE *)&decval);
dbnullbind(dbproc,1,&null_val);
for(;;)
{
do ret=dbnextrow(dbproc);
while(ret != NO_MORE_ROWS && ret != REG_ROW && ret != FAIL);
if (ret == NO_MORE_ROWS)
break;
if(null_val != -1)
{
if(ret != FAIL)
{
dbconvert(NULL,SQLNUMERIC,(BYTE*)&decval,sizeof(DBNUMERIC),SQLCHAR,converte printf ("%s\n", converted); // Close the connection to SQL Server. return (0);
}
else
printf("%s\n","FAILED CONVERSION");
}
}
}
dbexit ();
int err_handler (PDBPROCESS dbproc, INT severity,
INT dberr, INT oserr, LPCSTR dberrstr, LPCSTR oserrstr)
{
printf ("DB-Library Error %i: %s\n", dberr, dberrstr);
if (oserr != DBNOERR)
{
printf ("Operating System Error %i: %s\n", oserr, oserrstr);
}
return (INT_CANCEL);
int msg_handler (PDBPROCESS dbproc, DBINT msgno, INT msgstate,
INT severity, LPCSTR msgtext, LPCSTR server,
LPCSTR procedure, DBUSMALLINT line)
{
printf ("SQL Server Message %ld: %s\n", msgno, msgtext);
return (0);