How to get a SQL Server stored procedure return value using pyodbc?

后端 未结 3 930
囚心锁ツ
囚心锁ツ 2021-01-05 12:22

My team\'s using a python-based wiki server that calls stored procedures on a SQL Server database. Ideally, we\'d like to return integer values (1,0,-1) from the stored proc

相关标签:
3条回答
  • 2021-01-05 12:58

    I'm using this:

    def CallStoredProc(conn, procName, *args):
        sql = """DECLARE @ret int
                 EXEC @ret = %s %s
                 SELECT @ret""" % (procName, ','.join(['?'] * len(args)))
        return int(conn.execute(sql, args).fetchone()[0])
    

    It only works on SQL Server (or maybe Sybase), but it's a decent workaround.

    0 讨论(0)
  • 2021-01-05 12:59

    I added the following to get it to work in my application:

    def CallStoredProc(conn, procName, *args):
        sql = """SET NOCOUNT ON;
             DECLARE @ret int
             EXEC @ret = %s %s
             SELECT @ret""" % (procName, ','.join(['?'] * len(args)))
        return int(conn.execute(sql, args).fetchone()[0])
    
    0 讨论(0)
  • 2021-01-05 13:18

    Here's some relevant information: http://code.google.com/p/pyodbc/wiki/StoredProcedures

    It sounds like return values are still not supported, so you'll need to use a SELECT.

    0 讨论(0)
提交回复
热议问题