Microsoft KB Archive/812554

= FIX: ODBC Driver for SQL Server Converts Double Value to SQL_NUMERIC Value Incorrectly =

Article ID: 812554

Article Last Modified on 10/11/2005

-

APPLIES TO


 * Microsoft Data Access Components 2.7 Service Pack 1
 * Microsoft Data Access Components 2.7
 * Microsoft Data Access Components 2.6 Service Pack 2

-





SYMPTOMS
When an application that uses the Microsoft ODBC Driver for SQL Server (Sqlsrv32.dll) to insert a double value, and that value is stored in a SQL Server table as a numeric field, the ODBC driver may insert incorrect values. The ODBC Driver for SQL Server incorrectly rounds the numeric value while converting that value from a double value.



CAUSE
This only occurs when the value of the numeric field falls in a certain range. For example, if an application inserts a numeric field value of 5,000,000,000.000000 (with a precision of 22 and scale of 6) as a double value, the ODBC Driver for SQL Server inserts the value as 5,000,000,000.000001.



RESOLUTION
A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next MDAC 2.7 service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this fix has the file attributes (or later) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.

Hotfix file versions for MDAC 2.7 Service Pack 1 (SP1)
  Date         Time   Version          Size     File name --  06-Mar-2003  19:49  2000.81.9031.35   24,576  Odbcbcp.dll 06-Mar-2003 19:49  2000.81.9031.35  380,928  Sqlsrv32.dll

Hotfix file versions for MDAC 2.7 Refresh
  Date         Time   Version          Size     File name --  14-Apr-2003  18:01  2000.81.9001.25   24,576  Odbcbcp.dll 14-Apr-2003 18:00  2000.81.9001.25  356,352  Sqlsrv32.dll

Hotfix file versions for MDAC 2.6 Service Pack 2 (SP2)
  Date         Time   Version        Size     File name 15-Mar-2003 03:18  2000.80.739.0   29,252  Odbcbcp.dll 15-Mar-2003 03:01  2000.80.739.0  455,236  Sqlsrv32.dll



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the problem
  Run the following SQL statements in SQL Query Analyzer: USE Northwind CREATE TABLE NumericTestTbl(fldNum numeric(22,6)) GO  In Visual Studio .NET, create a new Visual C++ Win 32 Project (console application). Create a new C++ source file. To do this, follow these steps:  On the File menu, point to New, and then click File.</li> In the New File dialog box, click Visual C++ under Categories, and then click C++ File (.cpp) under Templates.</li></ol> </li>  Paste the following code in the file: // The include files // MACROs // // The main function // void main(int argc, char* argv[]) {   SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; SQLRETURN nstatus; SQLINTEGER ilen = 0; double dOutoutData = 0; printf(&quot;Converting Numeric to Double.\n&quot;); printf(&quot;==========================\n&quot;); // Allocate the Environment Handle nstatus = SQLAllocHandle(SQL_HANDLE_ENV,NULL,&henv); CHECK_SQLRETURN(nstatus, &quot;Unable to Allocate the Environment Handle.\n&quot;) // Set the ODBC Version Environment Attribute nstatus = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0); CHECK_SQLRETURN(nstatus, &quot;Unable to Set the ODBC Version Environment Attribute.\n&quot;) // Allocate the Connection Handle nstatus = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc); CHECK_SQLRETURN(nstatus, &quot;Unable to Allocate the Connection Handle.\n&quot;) // Connect to SQL Server nstatus = SQLDriverConnect(hdbc,                       NULL,                        (SQLCHAR*) &quot;Driver=SQL Server;Trusted Connection=Yes;Server=<My_SQL_Server>;&quot;,                        SQL_NTS,                        NULL,                        0,                        NULL,                        SQL_DRIVER_NOPROMPT); CHECK_SQLRETURN(nstatus, &quot;Unable to Connect to SQL Server.\n&quot;) // Allocate the Statement Handle nstatus = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); CHECK_SQLRETURN(nstatus, &quot;Unable to Allocate a Statement Handle.\n&quot;) // Prepare the INSERT statement nstatus = SQLPrepare(hstmt,(SQLCHAR*) &quot;INSERT INTO Northwind..NumericTestTbl (fldNum) VALUES(?)&quot;, SQL_NTS); CHECK_SQLRETURN(nstatus, &quot;Unable to Prepare the INSERT statement.\n&quot;) // Bind the Parameter dOutoutData = 5000000000.000000; nstatus = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_NUMERIC, 22, 6, (SQLPOINTER) &dOutoutData, 0,  &ilen); CHECK_SQLRETURN(nstatus, &quot;Unable to Bind the Parameter.\n&quot;) // Insert the data nstatus = SQLExecute(hstmt); CHECK_SQLRETURN(nstatus, &quot;Unable to Insert the data.\n&quot;) printf(&quot;Inserted: Record - '%lf'\n&quot;,dOutoutData); // Free the statement nstatus = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_SQLRETURN(nstatus, &quot;Unable to Free the Statement.\n&quot;) // Prepare the SELECT statement nstatus = SQLPrepare(hstmt, (SQLCHAR *)&quot;SELECT * from Northwind..NumericTestTbl&quot;, SQL_NTS); CHECK_SQLRETURN(nstatus, &quot;Unable to Prepare the SELECT statement.\n&quot;) // Execute a SELECT Statement nstatus = SQLExecute(hstmt); CHECK_SQLRETURN(nstatus, &quot;Unable to Execute a SELECT Statement.\n&quot;) // Fetch the data nstatus = SQLFetch(hstmt); CHECK_SQLRETURN(nstatus, &quot;Unable to Fetch the data.\n&quot;) // Get Record 1 nstatus = SQLGetData(hstmt, 1, SQL_C_DOUBLE, &dOutoutData, sizeof(dOutoutData), &ilen); CHECK_SQLRETURN(nstatus, &quot;Unable to Get Record 1.\n&quot;) printf(&quot;Retrieved: Record - '%lf'\n&quot;,dOutoutData); // Free the Statement nstatus = SQLFreeStmt(hstmt, SQL_CLOSE); CHECK_SQLRETURN(nstatus, &quot;Unable to Free the Statement.\n&quot;) // Exit printf(&quot;Press any key to Exit:&quot;); getch; } </li> Compile and run the application.</li>  Change <My_SQL_Server> to the name of your SQL Server.
 * 1) include <windows.h> // For windows apps.
 * 2) include <sql.h>         // ODBC Core Functions
 * 3) include <sqlext.h>    // Microsoft SQL Extensions
 * 4) include <stdio.h>      // For printf(...)
 * 5) include <conio.h>      // For getch
 * 1) define CHECK_SQLRETURN(nstatus, msgStr) if (nstatus != SQL_SUCCESS && nstatus != SQL_SUCCESS_WITH_INFO) {  printf(msgStr);    return; }

You see the following output: Converting Numeric to Double.

=
============= Inserted: Record - '5000000000.000000' Retrieved: Record - '5000000000.000001' Press any key to Exit: </li></ol>

After you install the hotfix, you see the following output: Converting Numeric to Double.

=
============= Inserted: Record - '5000000000.000000' Retrieved: Record - '5000000000.000000' Press any key to Exit:

Keywords: kbbug kbfix kbqfe kbhotfixserver KB812554

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.