我使用以下代码从 Python 执行 SQL 存储过程。所讨论的存储过程迭代另一个表中包含的许多不同的插入语句。但是,当从 Python 运行时,存储过程不会迭代完整的数据集。
我自己在 SQL 中测试了存储过程,它工作得很好。我还尝试编辑 sql_engine 连接上的 pool_timeout 参数,但这似乎也没有改变任何东西。
如何确保下面的代码继续执行,直到整个存储过程完成?
import connection_manager as cm
from sqlalchemy import text
sql_engine = cm.create_db_engine('Server','Database')
with sql_engine.connect() as conn:
conn.execute(text("EXEC dbo.SomeStoredProcedure"))
conn.commit()
conn.close()
使用SQLAlchemy提供的callproc()方法。 callproc() 方法允许您执行存储过程并检索该过程返回的任何结果集或输出参数。
import connection_manager as cm
from sqlalchemy import text
sql_engine = cm.create_db_engine('Server', 'Database')
with sql_engine.connect() as conn:
result = conn.execute(text("CALL dbo.SomeStoredProcedure()"))
conn.commit()
# Fetch the result sets, if any
while True:
row = result.fetchone()
if row is None:
break
print(row)
conn.close()