pandas + pyodbc ODBC SQL type -150 is not yet supported
If you can't change the driver, you'll need to change the query to return data types it supports.
SQL type -150 is SQL_VARIANT
, which is returned by SERVERPROPERTY
. The workaround is to explicitly CAST
the column to a supported type like nvarchar
:
CAST(SERVERPROPERTY('machinename') AS nvarchar(100)) AS 'Server Name',CAST(ISNULL(SERVERPROPERTY('instancename'),SERVERPROPERTY('machinename')) AS nvarchar(100)) AS 'Instance Name',
If it would be difficult to change the existing queries to explicitly CAST or CONVERT the troublesome values then you might consider trying to use a pyodbc Output Converter Function. It enables you to define a Python function that will be applied to the raw bytes returned for a given ODBC SQL type.
For example, this test code fails with the error you describe:
import pyodbccnxn = pyodbc.connect('DSN=SQLmyDb', autocommit=True)crsr = cnxn.cursor()server_name = crsr.execute("SELECT SERVERPROPERTY('machinename')").fetchval()print(server_name)crsr.close()cnxn.close()
but this works correctly for me under Python3
import pyodbcdef handle_sql_variant_as_string(value): return value.decode('utf-16le')cnxn = pyodbc.connect('DSN=SQLmyDb', autocommit=True)crsr = cnxn.cursor()cnxn.add_output_converter(-150, handle_sql_variant_as_string)server_name = crsr.execute("SELECT SERVERPROPERTY('machinename')").fetchval()print(server_name)crsr.close()cnxn.close()