Skip to content

Commit 549794e

Browse files
authored
Fix VARCHAR(MAX) fields from SQL Server returning no data (#271)
In MS SQL Server, a field defined as VARCHAR(MAX) will return a column size of 0, which breaks the SQLBindCol workflow. When we detect that a column size of 0 is returned for SQL_VARCHAR, SQL_WVARCHAR, and SQL_WVARBINARY, go through the 'long_data' path, which uses SQLGetData. Signed-off-by: Mark Irish <[email protected]>
1 parent cb229f9 commit 549794e

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/odbc_connection.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,8 +3045,14 @@ bind_buffers
30453045
case SQL_BINARY:
30463046
case SQL_VARBINARY:
30473047
{
3048-
column->buffer_size = column->ColumnSize;
30493048
column->bind_type = SQL_C_BINARY;
3049+
// Fixes a known issue with SQL Server and (max) length fields
3050+
if (column->ColumnSize == 0)
3051+
{
3052+
column->is_long_data = true;
3053+
break;
3054+
}
3055+
column->buffer_size = column->ColumnSize;
30503056
data->bound_columns[i].buffer =
30513057
new SQLCHAR[column->buffer_size * data->fetch_size]();
30523058
break;
@@ -3055,10 +3061,15 @@ bind_buffers
30553061
case SQL_WCHAR:
30563062
case SQL_WVARCHAR:
30573063
{
3058-
3064+
column->bind_type = SQL_C_WCHAR;
3065+
// Fixes a known issue with SQL Server and (max) length fields
3066+
if (column->ColumnSize == 0)
3067+
{
3068+
column->is_long_data = true;
3069+
break;
3070+
}
30593071
size_t character_count = column->ColumnSize + 1;
30603072
column->buffer_size = character_count * sizeof(SQLWCHAR);
3061-
column->bind_type = SQL_C_WCHAR;
30623073
data->bound_columns[i].buffer =
30633074
new SQLWCHAR[character_count * data->fetch_size]();
30643075
break;
@@ -3068,9 +3079,15 @@ bind_buffers
30683079
case SQL_VARCHAR:
30693080
default:
30703081
{
3082+
column->bind_type = SQL_C_CHAR;
3083+
// Fixes a known issue with SQL Server and (max) length fields
3084+
if (column->ColumnSize == 0)
3085+
{
3086+
column->is_long_data = true;
3087+
break;
3088+
}
30713089
size_t character_count = column->ColumnSize * MAX_UTF8_BYTES + 1;
30723090
column->buffer_size = character_count * sizeof(SQLCHAR);
3073-
column->bind_type = SQL_C_CHAR;
30743091
data->bound_columns[i].buffer =
30753092
new SQLCHAR[character_count * data->fetch_size]();
30763093
break;

0 commit comments

Comments
 (0)