Pyodbc-读取存储过程的输出参数(SQL Server)

2022-04-24 00:00:00 python pyodbc sql-server

我正在尝试使用pyodbc获取SQL Server存储过程的输出参数。该过程位于SQL Server中,我可以从我的程序中成功插入数据。这是SP的一部分:

INSERT INTO Table(a,b,c,d,e,f,g) 
VALUES (@a,@b,@c,@d,@e,@f,@g);
SET @Result = 'Inserted'
RETURN @Result

当我尝试读取代码中的结果变量时,它显示为空。


解决方案

这是来自python的调用,我使用的是pyodbc:

cursor = self.db.cursor()

sql = """
DECLARE @out nvarchar(max);
EXEC [dbo].[storedProcedure] @x = ?, @y = ?, @z = ?,@param_out = @out OUTPUT;
SELECT @out AS the_output;
"""

cursor.execute(sql, (x, y, z))
row = cursor.fetchone()
print(row[0])

然后使用输出参数创建存储过程。

相关文章