@@ -4094,6 +4094,10 @@ SQLRETURN FetchMany_wrap(SqlHandlePtr StatementHandle, py::list& rows, int fetch
40944094 // Reset attributes before returning to avoid using stack pointers later
40954095 SQLSetStmtAttr_ptr (hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)1 , 0 );
40964096 SQLSetStmtAttr_ptr (hStmt, SQL_ATTR_ROWS_FETCHED_PTR, NULL , 0 );
4097+
4098+ // Unbind columns to allow subsequent fetchone() calls to use SQLGetData
4099+ SQLFreeStmt_ptr (hStmt, SQL_UNBIND);
4100+
40974101 return ret;
40984102}
40994103
@@ -4228,6 +4232,9 @@ SQLRETURN FetchAll_wrap(SqlHandlePtr StatementHandle, py::list& rows,
42284232 // Reset attributes before returning to avoid using stack pointers later
42294233 SQLSetStmtAttr_ptr (hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)1 , 0 );
42304234 SQLSetStmtAttr_ptr (hStmt, SQL_ATTR_ROWS_FETCHED_PTR, NULL , 0 );
4235+
4236+ // Unbind columns to allow subsequent fetchone() calls to use SQLGetData
4237+ SQLFreeStmt_ptr (hStmt, SQL_UNBIND);
42314238
42324239 return ret;
42334240}
@@ -4254,12 +4261,21 @@ SQLRETURN FetchOne_wrap(SqlHandlePtr StatementHandle, py::list& row,
42544261 SQLRETURN ret;
42554262 SQLHSTMT hStmt = StatementHandle->get ();
42564263
4264+ // Unbind any columns from previous fetch operations (e.g., fetchmany)
4265+ // to avoid conflicts with SQLGetData. SQLGetData cannot be used on
4266+ // columns that are already bound.
4267+ SQLFreeStmt_ptr (hStmt, SQL_UNBIND);
4268+
42574269 // Assume hStmt is already allocated and a query has been executed
42584270 ret = SQLFetch_ptr (hStmt);
42594271 if (SQL_SUCCEEDED (ret)) {
42604272 // Retrieve column count
42614273 SQLSMALLINT colCount = SQLNumResultCols_wrap (StatementHandle);
42624274 ret = SQLGetData_wrap (StatementHandle, colCount, row, charEncoding, wcharEncoding);
4275+ if (!SQL_SUCCEEDED (ret)) {
4276+ LOG (" FetchOne_wrap: Error retrieving data with SQLGetData - SQLRETURN=%d" , ret);
4277+ return ret;
4278+ }
42634279 } else if (ret != SQL_NO_DATA) {
42644280 LOG (" FetchOne_wrap: Error when fetching data - SQLRETURN=%d" , ret);
42654281 }
0 commit comments