简体   繁体   English

有没有办法将存储过程 Return_Value 返回给执行它的 python 脚本?

[英]Is there a way to return a Stored Procedure Return_Value to the python script executing it?

I am writing a python script that should update a log table and notify me of it.我正在编写一个 python 脚本,它应该更新日志表并通知我。 Part of this script is to execute a SQL Server stored procedure.该脚本的一部分是执行 SQL 服务器存储过程。 I get an email telling me the status of the script as well as the status of the Stored procedure depending on the return value.我得到一个 email 告诉我脚本的状态以及存储过程的状态,具体取决于返回值。 But I can't seem to get my stored procedure to return the Return_Value.但我似乎无法让我的存储过程返回 Return_Value。 The script executes the stored procedure but receives no feedback.该脚本执行存储过程但未收到任何反馈。

I have tried using SQLAlchemy:我试过使用 SQLAlchemy:

resulta = engine.execute(text(""" DECLARE @return int EXEC @return = [MYPROC] '{User}', '{Status}' 
                         SELECT 'RETURN' = @return """.format(User = curruser, Status = 'Pass')).execution_options(autocommit=True))

My resulta returns: <sqlalchemy.engine.cursor.LegacyCursorResult object at 0xXXXXXXXXXX>我的结果返回: <sqlalchemy.engine.cursor.LegacyCursorResult object at 0xXXXXXXXXXX>

I have also tried using cursors:我也尝试过使用游标:

resultb = cur.execute(""" DECLARE @return int EXEC @return = [MYPROC] '{User}', '{Status}' 
                         SELECT 'RETURN' = @return """.format(User = curruser, Status = 'Pass'))

My resultb returns: NoneType object我的结果返回: NoneType object

I have also tried replacing the SELECT with a RETURN but that seems to cause more issues.我也尝试用 RETURN 替换 SELECT 但这似乎会导致更多问题。 Please help.请帮忙。

You certainly can do this using pyodbc Cursors.您当然可以使用 pyodbc Cursors 来做到这一点。 You need to know in advance how many result sets are returned by the stored procedure that you're executing so that the result set with the return code is where you expect.您需要事先知道您正在执行的存储过程返回了多少结果集,以便带有返回代码的结果集位于您期望的位置。

The following example executes sp_helpdb with a @dbname parameter, which returns two result sets from the stored procedure itself and the third result set holds the return code...下面的示例使用@dbname参数执行sp_helpdb ,它从存储过程本身返回两个结果集,第三个结果集包含返回代码...

import pyodbc

server = 'tcp:127.0.0.1,1433' 
database = 'master' 
username = 'sa' 
password = 'StrongPassw0rd' 

connStr = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password
cnxn = pyodbc.connect(connStr)
cursor = cnxn.cursor()

params = ( ('tempdb') )
result = cursor.execute("""
    declare @ReturnCode int;
    exec @ReturnCode = sp_helpdb @dbname = ?;
    select @ReturnCode as ReturnCode;
""", params)

# 1st result set from stored procedure...
databaseColumns = tuple([column[0] for column in result.description])
databaseRows = result.fetchall()
print(databaseColumns)
print(databaseRows)
print()

result.nextset()

# 2nd result set from stored procedure...
logicalFileColumns = tuple([column[0] for column in result.description])
logicalFileRows = result.fetchall()
print(logicalFileColumns)
print(logicalFileRows)
print()

result.nextset()

# 3rd result set for return code...
returnCodeColumns = tuple([column[0] for column in result.description])
returnCodeRows = result.fetchall()
print(returnCodeColumns)
print(returnCodeRows)
print()

That outputs the following to the console...将以下内容输出到控制台...

('name', 'db_size', 'owner', 'dbid', 'created', 'status', 'compatibility_level')
[
  ('tempdb', '     24.00 MB', 'sa', 2, 'Apr 29 2022', 'Status=ONLINE, Updateability=READ_WRITE, UserAccess=MULTI_USER, Recovery=SIMPLE, Version=904, Collation=SQL_Latin1_General_CP1_CI_AS, SQLSortOrder=52, IsAutoCreateStatistics, IsAutoUpdateStatistics', 150)
]

('name', 'fileid', 'filename', 'filegroup', 'size', 'maxsize', 'growth', 'usage')
[
  ('tempdev', 1, '/var/opt/mssql/data/tempdb.mdf', 'PRIMARY', '8192 KB', 'Unlimited', '65536 KB', 'data only'),
  ('templog', 2, '/var/opt/mssql/data/templog.ldf', None, '8192 KB', 'Unlimited', '65536 KB', 'log only'),
  ('tempdev2', 3, '/var/opt/mssql/data/tempdb2.ndf', 'PRIMARY', '8192 KB', 'Unlimited', '65536 KB', 'data only')
]

('ReturnCode',)
[(0, )]

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

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