pymssql如何传递输出参数

时间:2017-12-23 12:53:41

标签: python sql-server pymssql

我尝试过使用此代码:

   outputparam1 = pymssql.output(int)
   outputparam2=pymssql.output(int)
   outputparam3=pymssql.output(int)

    try:
        with pymssql.connect('localhost','sa','anypassword', 'anydb') as conn:
            with conn.cursor(as_dict=True) as cursor:
                cursor.callproc("storeprocedure_name",('inputparam',outputparam1,outputparam2,outputparam3))
    except Exception,e:
        pass

并在mssql storeprocedure_name:

ALTER PROCEDURE [dbo].[storeprocedure_name](@inputparam NVarChar(100),                                         
                                       @outputparam1 Int Output,
                                       @outputparam2 Int Output,
                                       @outputparam3 Int Output) As begin end;

上面的代码仍会抛出异常“expect parameter @ outputparam1 ...”

任何帮助都会非常感谢,谢谢

1 个答案:

答案 0 :(得分:2)

cursor.callproc('storedprocedure_name', (first_param, second_param, an_output_param,))

然后返回的值将出现在" an_output_param"变量。

在你的情况下,你应该试试这个。

cursor.callproc("storedprocedure_name", ('inputparam', outputparam1, outputparam2, outputparam3,))

试试这个

sqlcmd = """
DECLARE @outputparam1 INT, @outputparam2 INT, @outputparam3 INT

EXEC storedprocedure_name
     %s, @outputparam1 OUT, @outputparam2 OUT, @outputparam3 OUT

SELECT @outputparam1, @outputparam2, @outputparam3
"""
res = conn.execute_row(sqlcmd, ('inputparam'))