How do you get output parameters from a stored procedure in Python?

前端 未结 9 1799
既然无缘
既然无缘 2020-12-20 13:40

I\'ve googled around a bit, but maybe I didn\'t put the correct magik incantation into the search box.

Does anyone know how to get output parameters from a stored p

9条回答
  •  一整个雨季
    2020-12-20 14:14

    If you cannot or don't want to modify the original procedure and have access to the database you can write a simple wrapper procedure that is callable from python.

    For example, if you have a stored procedure like:

    CREATE PROC GetNextNumber
       @NextNumber int OUTPUT
    AS
    ...
    

    You could write a wrapper like so which is easily callable from python:

    CREATE PROC GetNextNumberWrap
    AS
        DECLARE @RNextNumber int
        EXEC GetNextNumber @RNextNumber
        SELECT @RNextNumber
    GO
    

    Then you could call it from python like so:

    import pymssql
    con = pymssql.connect(...)
    cur = con.cursor()
    cur.execute("EXEC GetNextNumberWrap")
    next_num = cur.fetchone()[0]
    

提交回复
热议问题