pandas + pyodbc目前尚不支持ODBC SQL类型-150

时间:2017-08-14 14:55:59

标签: sql-server pandas odbc pyodbc

我知道有很多主题,但我认为这是非常具体的。 我得到了当前用于审计目的的代码:

import pandas as pd
import pyodbc

query = """
--Top 50 high total CPU Queries
SELECT TOP 50
'High CPU Queries' as Type,
serverproperty('machinename') as 'Server Name',
isnull(serverproperty('instancename'),serverproperty('machinename')) as 'Instance Name',
        COALESCE(DB_NAME(qt.dbid),
        DB_NAME(CAST(pa.value as int)), 
        'Resource') AS DBNAME,
    qs.execution_count as [Execution Count],
    qs.total_worker_time/1000 as [Total CPU Time],
    (qs.total_worker_time/1000)/qs.execution_count as [Avg CPU Time],
    qs.total_elapsed_time/1000 as [Total Duration],
    (qs.total_elapsed_time/1000)/qs.execution_count as [Avg Duration],
    qs.total_physical_reads as [Total Physical Reads],
    qs.total_physical_reads/qs.execution_count as [Avg Physical Reads],
    qs.total_logical_reads as [Total Logical Reads],
    qs.total_logical_reads/qs.execution_count as [Avg Logical Reads],
SUBSTRING(qt.text,qs.statement_start_offset/2, 
        (case when qs.statement_end_offset = -1 
        then len(convert(nvarchar(max), qt.text)) * 2 
        else qs.statement_end_offset end -qs.statement_start_offset)/2)
        as query_text     
    FROM sys.dm_exec_query_stats qs
    cross apply sys.dm_exec_sql_text(qs.sql_handle) as qt
    outer apply sys.dm_exec_query_plan (qs.plan_handle) qp
    outer APPLY sys.dm_exec_plan_attributes(qs.plan_handle) pa
    where attribute = 'dbid'   
    ORDER BY 
        [Total CPU Time] DESC
"""
cnxn = pyodbc.connect('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
cnxn.execute(query).fetchall()
cnxn.close()

我收到以下错误:

  
    
      
        

cnxn.execute(sql_status_20).fetchall()         Traceback(最近一次调用最后一次):           文件"",第1行,in         pyodbc.ProgrammingError :(' ODBC SQL类型-150尚不支持.column-index = 1 type = -150',' HY106')

      
    
  

任何人都可以帮我处理这件事吗?我使用日期的许多SQL Server审核脚本遇到了同样的问题,我无法更改驱动程序,因为我的生产环境中包含所有类型的SQL版本。

2 个答案:

答案 0 :(得分:4)

如果无法更改驱动程序,则需要更改查询以返回它支持的数据类型。

SQL类型-150是SQL_VARIANT,由SERVERPROPERTY返回。解决方法是明确CAST列到支持的类型,如nvarchar

CAST(SERVERPROPERTY('machinename') AS nvarchar(100)) AS 'Server Name',
CAST(ISNULL(SERVERPROPERTY('instancename'),SERVERPROPERTY('machinename')) AS nvarchar(100)) AS 'Instance Name',

答案 1 :(得分:1)

如果难以将现有查询更改为显式CAST或CONVERT这些麻烦的值,那么您可以考虑尝试使用pyodbc Output Converter Function。它使您能够定义将应用于为给定ODBC SQL类型返回的原始字节的Python函数。

例如,此测试代码因您描述的错误而失败:

import pyodbc

cnxn = pyodbc.connect('DSN=SQLmyDb', autocommit=True)
crsr = cnxn.cursor()

server_name = crsr.execute("SELECT SERVERPROPERTY('machinename')").fetchval()
print(server_name)

crsr.close()
cnxn.close()

但这在Python3

下适合我
import pyodbc


def 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()