[英]How to get the value returned by a stored procedure called from python in SQL server 2008 R2
I need to check the returned value from a stored procedure on SQL server 2008 R2. 我需要检查SQL Server 2008 R2上存储过程的返回值。
import pyodbc
sql_str = """
DECLARE @return_value int
SET @return_value = -1
INSERT INTO [my_database].[dbo].[my_table]
EXEC @return_value = [my_database].[dbo].[my_stored_procedure]
if @return_value <> 0
BEGIN
EXEC sys.sp_addmessage 60000, 16, ' test sp returns wrong code ! '
RAISERROR (60000, 16, 1)
END
"""
sql_str_connect_db = " DRIVER={SQL server};SERVER={my_server};DATABASE={my_db};UID=my_id;PWD=my_password "
cnxn = pyodbc.connect(sql_str_connect_db )
cursor_test = cnxn.cursor()
cursor_test.execute(sql_str)
cnxn_test.commit()
But, I got error: 但是,我得到了错误:
pyodbc.Error: ('HY007', '[HY007] [Microsoft][ODBC SQL Server Driver]Associated statement is not prepared (0) (SQLNumResultCols)') pyodbc.Error :(“ HY007”,“ [HY007] [Microsoft] [ODBC SQL Server驱动程序]关联语句未准备好(0)(SQLNumResultCols)”)
And, how to check the value of "@return_value" in python ? 并且,如何在python中检查“ @return_value”的值?
After searching , I cannot find a solution. 搜索后,我找不到解决方案。
Thanks 谢谢
You should be able to just select it like this in your SQL: 您应该能够在SQL中像这样选择它:
SELECT @return_value AS return_value
Then use your cursor to fetch the records: 然后使用光标获取记录:
rows = cursor_test.fetchall()
for row in rows:
print(row.return_value)
Regards, 问候,
-Tim -蒂姆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.