Pyodbc 无法在 SQL Server 2008 中执行 proc

Pyodbc unable to execute proc in SQL Server 2008

上下文: 我有一个 Python 函数,它应该通过 Pyodbc 连接到 SQL Server 2008 并执行一个存储过程来解析 XML 文件并将其加载到 table。我正在使用 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 脚本似乎已成功连接到数据库,并且执行时没有任何错误。但是当我检查底层数据库 table 时,数据还没有被加载,这对我来说表明存储过程没有被执行。当我手动 运行 存储过程时,它 运行 没有任何问题并加载数据。

问题:有人可以提供疑难解答提示并帮助我理解程序未执行的原因吗?我想它可能正在执行该过程,但无论出于何种原因,并非所有 SQL 都可能 运行ning?或者在过程中执行 EXEC 调用后可能是光标 exits/returns?

这是 SQL 服务器中的存储过程:

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 

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