繁体   English   中英

Python & SQL 服务器存储过程挂起

[英]Python & SQL Server stored procedure hang

我试图让 Python 在我的 SQL 服务器中运行存储过程,该过程启动了一系列过程,其中涉及导入文件处理它并输出几个文件。

到目前为止,我已经获得了我的代码,因此它可以接受对表的输入,但是当它调用存储过程时,整个事情都会挂起。

检查服务器上的Who2 ,它正在等待preemptive_OS_Pipeops ,搜索显示它正在等待 SQL 服务器之外的东西完成,然后再继续。

如果可以使用 pyodbc 盲激活存储过程然后关闭连接,是否有人能够阐明?

我的信念是只需告诉程序运行然后关闭就可以解决问题,但是我在查找此代码时遇到问题

Python代码:

    connection2 = pypyodbc.connect('Driver={SQL Server}; Server=server;Database=db', timeout=1)
    cursor2 = connection2.cursor()
    cursor2.execute("{CALL uspGoBabyGo}")
    connection2.close()
    return 'file uploaded successfully'

存储过程:

BEGIN
    SET NOCOUNT ON;

    EXECUTE [dbo].[uspCableMapImport]
END

搜索后脚本停止将记录发布到表中,我找到了问题的解决方案。 我需要在脚本中添加 autocommit=True 行,现在代码如下;

    connection = pyodbc.connect('Driver={SQL Server}; 
    Server='Server';Database='DB';Trusted_Connection=yes')
    connection.autocommit=True
    cursor = connection.cursor()
    referee = file.filename.rsplit('.')[0]
    SQLCommand = ("INSERT INTO RequestTable(Reference, Requested) VALUES ('"+ str(referee) +"', " + datetime.now().strftime('%Y-%m-%d') + ")")
    cursor.execute(SQLCommand)
    connection.commit
    SQLCommand2 = ("{CALL uspGoBabyGo}")
    cursor.execute(SQLCommand2)
    connection.commit
    connection.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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