Using SQL Server stored procedures from Python (pyodbc)

后端 未结 7 911
广开言路
广开言路 2020-12-30 00:23

I\'m have a stored procedure, code:

DECLARE @RC int 
DECLARE @id varchar(13) 
DECLARE @pw varchar(13) 
DECLARE @depart varchar(32) 
DECLARE @class varchar(12         


        
7条回答
  •  醉梦人生
    2020-12-30 00:52

    The accepted answer does not address the issue of capturing the return value from the stored procedure, which can be done like this:

    id_ = 'test' 
    pw = '12345' 
    depart = 'none' 
    class_ = 'GM' 
    name = 'name' 
    birthday = 'None' 
    grade = 3 
    subgrade = 2 
    
    sql = """\
    DECLARE @RC int;
    EXEC @RC = [my_database].[dbo].[my_sp] ?, ?, ?, ?, ?, ?, ?, ?;
    SELECT @RC AS rc;
    """
    values = (id_, pw, depart, class_, name, birthday, grade, subgrade)
    cursor.execute(sql, values)
    rc = cursor.fetchval()  # pyodbc convenience method similar to cursor.fetchone()[0]
    

提交回复
热议问题