Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo guys
I use this code to read a single column from a sql server table. the column's data type is text, containing 174 chars. I use smaller buffer size of 100 wchars to get the actual data size in StrLen_or_IndPtr. When BufferLength is <= 348 bytes, SQLGetData returns SQL_SUCCESS_WITH_INFO, SALSTATE is 01004(Indicating right truncation), 100 wchars is copied to my buffer, with a terminating null, but StrLen_or_IndPtr returns -4. when BufferLength >= 350 bytes, Every thing is okay, and StrLen_or_IndPtr returns 348.
every thing is okay and StrLen_or_IndPtr returns 350.
C++
wchar_t * wszQuery = L"SELECT [MATN] FROM [ATSETUP] WHERE [IDKEY] = \'AT:CODING\'";
SQLRETURN iRetCode;
iRetCode = ::SQLExecDirectW(hstmt, (SQLWCHAR*)wszQuery, SQL_NTS);
if (!SQL_SUCCEEDED(iRetCode)) return FALSE;
iRetCode = ::SQLFetch(hstmt);
if (iRetCode == SQL_NO_DATA) return TRUE;
if (!SQL_SUCCEEDED(iRetCode)) return FALSE;
SQLSMALLINT iSqlCType = SQL_C_WCHAR;
SQLSMALLINT iColumn = 1;
SQLLEN iOutputLen = 0;
const int BUFLEN = 100;
wchar_t wszBuffer[BUFLEN + 1];
iRetCode = ::SQLGetData(hstmt, iColumn, iSqlCType, wszBuffer, sizeof(wszBuffer), &OutputLen);
//returns 1, puts 100 wchars to wszBuffer, but returns -4 in StrLen_or_IndPtr.
if I use SQL_C_CHAR, this problem doesn't occur. I use sql-server 2012 express 64 bit.
Can any one help me?

Edit: This problem doesn't occur when data type of column is ntext or nvarchar(max). It occurs for text and varchar(max).

Edit: The problem is: SQLGetData refuses to return size of data in case converting from char to wchar, vice versa. leaving calculating size of data to programmer. This is so bad. returning the size of data would be very simpler and faster if returned by SQLGetData.

What I have tried:

I have tried the C code above.
Posted
Updated 21-Feb-16 3:53am
v3
Comments
Richard MacCutchan 21-Feb-16 3:25am    
You should not use sizeof with a wchar_t array, as it provides the size in bytes, not characters.
mr.abzadeh 21-Feb-16 5:11am    
SQLGetData requires BufferLength in bytes

1 solution

You should first check the value of iRetCode to see whether your call was fully successful. That would help you to understand that the value of -4 is the status indicator SQL_NO_TOTAL as described in SQLGetData Function[^], which tells you that the returned data was truncated. It always helps to read the documentation first.
 
Share this answer
 
Comments
mr.abzadeh 21-Feb-16 9:47am    
@Richard MacCutchan: I knew that. The question is: Why SQLGetData doesn't return the total sizeof data in bytes? so that programmer allocate a buffer and read data, without using tedious loops? It is very simple to return data size in StrLen_or_IndPtr, and previous versions of sql server/odbc driver ded this.
Richard MacCutchan 21-Feb-16 12:52pm    
Because that is how SQL has been designed and coded, as explained in the documentation. If you think that is wrong then you need to talk to the designers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900