Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion mssql_python/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def _map_sql_type( # pylint: disable=too-many-arguments,too-many-positional-arg
if param is None:
logger.debug("_map_sql_type: NULL parameter - index=%d", i)
return (
ddbc_sql_const.SQL_VARCHAR.value,
ddbc_sql_const.SQL_UNKNOWN_TYPE.value,
ddbc_sql_const.SQL_C_DEFAULT.value,
1,
0,
Expand Down Expand Up @@ -2185,6 +2185,16 @@ def executemany( # pylint: disable=too-many-locals,too-many-branches,too-many-s
min_val=min_val,
max_val=max_val,
)

# For executemany with all-NULL columns, SQL_UNKNOWN_TYPE doesn't work
# with array binding. Fall back to SQL_VARCHAR as a safe default.
if (
sample_value is None
and paraminfo.paramSQLType == ddbc_sql_const.SQL_UNKNOWN_TYPE.value
):
paraminfo.paramSQLType = ddbc_sql_const.SQL_VARCHAR.value
paraminfo.columnSize = 1

# Special handling for binary data in auto-detected types
if paraminfo.paramSQLType in (
ddbc_sql_const.SQL_BINARY.value,
Expand Down
15 changes: 10 additions & 5 deletions mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,19 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
hStmt, static_cast<SQLUSMALLINT>(paramIndex + 1), &describedType,
&describedSize, &describedDigits, &nullable);
if (!SQL_SUCCEEDED(rc)) {
// SQLDescribeParam can fail for generic SELECT statements where
// no table column is referenced. Fall back to SQL_VARCHAR as a safe default.
LOG("BindParameters: SQLDescribeParam failed for "
"param[%d] (NULL parameter) - SQLRETURN=%d",
"param[%d] (NULL parameter) - SQLRETURN=%d, falling back to SQL_VARCHAR",
paramIndex, rc);
return rc;
sqlType = SQL_VARCHAR;
columnSize = 1;
decimalDigits = 0;
} else {
sqlType = describedType;
columnSize = describedSize;
decimalDigits = describedDigits;
}
sqlType = describedType;
columnSize = describedSize;
decimalDigits = describedDigits;
}
dataPtr = nullptr;
strLenOrIndPtr = AllocateParamBuffer<SQLLEN>(paramBuffers);
Expand Down
Loading