如何从Python中的存储过程获取输出参数? [英] How do you get output parameters from a stored procedure in Python?
问题描述
我用Google搜索了一下,但是也许我没有在搜索框中输入正确的魔咒.
I've googled around a bit, but maybe I didn't put the correct magik incantation into the search box.
有人知道如何从Python的存储过程中获取输出参数吗?我正在使用pymssql调用存储过程,但不确定是否使用正确的语法来获取输出参数.我不认为可以使用任何其他数据库模块,因为我是从Linux机器上运行该模块来连接到MS Server上的mssql数据库的.
Does anyone know how to get output parameters from a stored procedure in Python? I'm using pymssql to call a stored procedure, and I'm not sure of the correct syntax to get the output parameter back. I don't think I can use any other db modules since I'm running this from a Linux box to connect to a mssql database on a MS Server.
import pymssql
con = pymssql.connect(host='xxxxx',user='xxxx',password='xxxxx',database='xxxxx')
cur = con.cursor()
query = "EXECUTE blah blah blah"
cur.execute(query)
con.commit()
con.close()
推荐答案
I'm not a python expert but after a brief perusing of the DB-API 2.0 I believe you should use the "callproc" method of the cursor like this:
cur.callproc('my_stored_proc', (first_param, second_param, an_out_param))
然后,您将在"an_out_param"变量中将结果存储在out参数的返回值中.
Then you'll have the result in the returned value (of the out param) in the "an_out_param" variable.
这篇关于如何从Python中的存储过程获取输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!