简体   繁体   中英

Pyodbc unable to execute proc in SQL Server 2008

Context: I have a Python function which is supposed to connect to SQL Server 2008 via Pyodbc and execute a stored procedure that parses an XML file and loads it to a table. I'm using Python 3.8.1 and I installed the updated ODBC Driver (ODBC Driver 13.1)

Here's the Pyhton function:

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

Problem: The Python script appears to be successfully connecting to the database, and executes without any errors. BUT when I check the underlying database table, the data hasn't been loaded which to me indicates that the stored procedure isn't being executed. When I run the stored procedure manually it runs without any issue and loads the data.

Questions: Can someone please assist in troubleshooting tips and help me understand why the procedure isnt being executed? I suppose its possible the procedure is being executed but for whatever reason not all the SQL may be running? Or perhaps the cursor exits/returns once the EXEC calls in the procedure are made?

Here's the stored procedure in 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 

@Parfait,您是对的——使用普通表而不是临时表时问题似乎已解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM