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

前端 未结 9 1796
既然无缘
既然无缘 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:11

    If you make your procedure produce a table, you can use that result as a substitute for out params.

    So instead of:

    CREATE PROCEDURE Foo (@Bar INT OUT, @Baz INT OUT) AS
    BEGIN
       /* Stuff happens here */
       RETURN 0
    END
    

    do

    CREATE PROCEDURE Foo (@Bar INT, @Baz INT) AS
    BEGIN
       /* Stuff happens here */
       SELECT @Bar Bar, @Baz Baz
       RETURN 0
    END
    
    0 讨论(0)
  • 2020-12-20 14:12

    Here's how I did it, the key is to declare output parameter first:

    import cx_Oracle as Oracle
    
    conn = Oracle.connect('xxxxxxxx')
    cur = conn.cursor()
    
    idd = cur.var(Oracle.NUMBER)
    cur.execute('begin :idd := seq_inv_turnover_id.nextval; end;', (idd,))
    print(idd.getvalue())
    
    0 讨论(0)
  • 2020-12-20 14:14

    I'm not a python expert but after a brief perusing of the DB-API 2.0 I believe you should use the "callproc" method of the cursor like this:

    cur.callproc('my_stored_proc', (first_param, second_param, an_out_param))
    

    Then you'll have the result in the returned value (of the out param) in the "an_out_param" variable.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2020-12-20 14:21

    I was able to get an output value from a SQL stored procedure using Python. I could not find good help getting the output values in Python. I figured out the Python syntax myself, so I suspect this is worth posting here:

    import sys, string, os, shutil, arcgisscripting
    from win32com.client import Dispatch
    from adoconstants import *
    
    #skip ahead to the important stuff
    
    conn = Dispatch('ADODB.Connection')
    conn.ConnectionString = "Provider=sqloledb.1; Data Source=NT38; Integrated Security = SSPI;database=UtilityTicket"
    conn.Open()
    
    #Target Procedure Example: EXEC TicketNumExists @ticketNum = 8386998, @exists output
    
    Cmd = Dispatch('ADODB.Command')
    Cmd.ActiveConnection = conn
    
    Cmd.CommandType = adCmdStoredProc
    Cmd.CommandText = "TicketNumExists"
    
    Param1 = Cmd.CreateParameter('@ticketNum', adInteger, adParamInput)
    Param1.Value = str(TicketNumber)
    Param2 = Cmd.CreateParameter('@exists', adInteger, adParamOutput)
    
    Cmd.Parameters.Append(Param1)
    Cmd.Parameters.Append(Param2)
    
    Cmd.Execute()
    
    Answer = Cmd.Parameters('@exists').Value
    
    0 讨论(0)
  • 2020-12-20 14:21

    You might also look at using SELECT rather than EXECUTE. EXECUTE is (iirc) basically a SELECT that doesn't actually fetch anything (, just makes side-effects happen).

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