【问题标题】:Pyodbc unable to execute proc in SQL Server 2008Pyodbc 无法在 SQL Server 2008 中执行 proc
【发布时间】:2020-02-19 21:05:58
【问题描述】:

上下文: 我有一个 Python 函数,它应该通过 Pyodbc 连接到 SQL Server 2008 并执行一个解析 XML 文件并将其加载到表中的存储过程。我正在使用 Python 3.8.1 并安装了更新的 ODBC 驱动程序(ODBC 驱动程序 13.1)

这是 Pyhton 函数:

import pyodbc

def load_to_database():
    username = 'REDACTED'
    password = 'REDACTED'

    connection = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
                        "Server=REDACTED;"
                        "Database=REDACTED;"
                        "Username="+username+";"
                        "Password="+password+";"
                        "Trusted_Connection=yes;")
    connection_cursor = connection.cursor()

    executesp = """EXEC [REDACTED].[dbo].[get_cp_api_data_part_search]"""
    connection.autocommit = True
    connection_cursor.execute(executesp)
    connection.close()
load_to_database()

问题: Python 脚本似乎已成功连接到数据库,并且执行时没有任何错误。但是当我检查底层数据库表时,数据还没有加载,这对我来说表明存储过程没有被执行。当我手动运行存储过程时,它会毫无问题地运行并加载数据。

问题:有人可以提供故障排除提示并帮助我了解为什么没有执行该过程吗?我想它可能正在执行该过程,但无论出于何种原因,并非所有 SQL 都可能正在运行?或者,一旦过程中的 EXEC 调用完成,光标可能退出/返回?

这是 SQL Server 中的存储过程:

IF OBJECT_ID('tempdb..##search') IS NOT NULL DROP TABLE ##earch

DECLARE @xml_data XML

--Use OPENROWSET to extract the data from the xml file. 
SELECT @xml_data=O
FROM OPENROWSET(BULK N'C:\Users\eb\Desktop\Important Docs & Links\Important Documents\xml_data_removed_tags.xml', SINGLE_BLOB) as file_output(O)

--Variable below will be used to create a recognizeable XML document within SQL Server memory
DECLARE @xml_doc int

--Procedure below takes 2 parameters: 1) output parameter to store handle to xml document and 2) the xml document itself 
EXEC sp_xml_preparedocument @xml_doc OUTPUT, @xml_data

--Function below parses the XML based on the hierarchy path used, list the attributes and elements you want to query
--Queried into temptable
SELECT *
INTO ##search
FROM OPENXML(@xml_doc,'REDACTED/*',2)
WITH (
        REDACTED nvarchar(10),
        REDACTED int, 
        REDACTED int, 
        REDACTED nvarchar(60),
        REDACTED nvarchar(10), 
        REDACTED int, 
        REDACTED nvarchar(30),
        REDACTED nvarchar(20),
        REDACTED nvarchar(20), 
        REDACTED int, 
        REDACTED nvarchar(5), 
        REDACTED nvarchar(5), 
        REDACTED int, 
        REDACTED nvarchar(50)'REDACTED',
        REDACTED nvarchar(25)'REDACTED',
        REDACTED int 'REDACTED', 
        REDACTED nvarchar(60) 'REDACTED', 
        REDACTED int 'REDACTED', 
        REDACTED nvarchar(50) 'REDACTED', 
        REDACTED nvarchar(20) 'REDACTED'
        )

--This procedure removes the saved prepared xml document 

【问题讨论】:

  • 当我检查底层数据库表时 ...你是怎么做到的?临时表仅在会话期间存在。你在临时表上运行另一个cursor.execute 吗?
  • 顺便说一下,IIUC,Python 作为一种通用语言,可以充分解析、修改、创建 XML 文件。如果不使用任何数据库数据,这感觉太麻烦了,无法通过 SQL Server 运行。
  • 顺便说一句,微软终止了对SQL Server 2008的支持

标签: python sql-server tsql stored-procedures pyodbc


【解决方案1】:

@Parfait,你是对的——当使用普通表而不是临时表时,问题似乎得到了解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    相关资源
    最近更新 更多