Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loss of decimal data using SQL Native Client ODBC

Status
Not open for further replies.

colmmchugh

Programmer
Oct 12, 2006
2
US
Hi,
I have a program that selects numeric data from a table in SQL Server and inserts it into another SQL Server table. The program is in MVC++ 7.0 and uses SQL Native Client ODBC to interact with the SQL Server database.

The problem is that the data is getting truncated when it is being inserted into the target table. For example, the number 23.0 in the source table will be 2.0 in the target table. Here is a more detailed description of the setup;

SQL Server database contents and SQL commands used by my program:

Table S: ( C1 numeric(18,0) )
Table T: ( C1 decimal(18,0) )

QUERY1: SELECT C1 FROM S
QUERY2: INSERT INTO T ( C1 ) VALUES (? )

ODBC calls used by my program:

SQLPrepare( QUERY1 ); // Prepare QUERY1
SQLExecute ;// Execute QUERY1
SQLBindCol( 1, SQL_C_WCHAR, baseAddr, 42, 0 ); // 1 column in result
SQLFetchScroll ;// Get the data into memory

Note: After the data has been fetched, it is in a 2 byte UNICODE (usc) form. Thus, the number 23 appears in memory like this:
0x3200 0x3300

SQLPrepare( QUERY2 );
SQLBindParameter( 1, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_CHAR, 42, 0, baseAddr, 42, 0) ; // Bind the parameter to it's type and size
SQLExecute ; // Do the actual insert

Note that my program is unicode enabled, hence SQL_C_WCHAR is used for the C data type in the calls to SQLBindCol and SQLBindParameter. I have tried using both SQL_CHAR and SQL_WCHAR as the SQL data type in the call to SQLBindParameter, but the behavior (described above) is the same in both cases. A colleague has the non-unicode version of this program working fine, so I'm not sure what I'm doing wrong, or what extra I need to be doing.

I came across the following link related to SQLBindParameter and UNICODE:
But I am getting the data back in UNICODE format, so have to conclude that I am using a UNICODE ODBC driver, so this does not apply to me. So this is turning into a real headscrathcher for me! Any helpful questions or tips greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top