Using SQL Server stored procedures from Python (pyodbc)

后端 未结 7 912
广开言路
广开言路 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]
    
    0 讨论(0)
  • 2020-12-30 00:55

    Another flavour of Gord's answer is using OUTPUT and named parameters (to be defined within the Stored procedure) for clarity.

    id_ = 'test' 
    pw = '12345' 
    depart = 'none' 
    class_ = 'GM' 
    name = 'name' 
    birthday = 'None' 
    grade = 3 
    subgrade = 2 
    
    sql = """\
    DECLARE @RC int;
    EXEC [my_database].[dbo].[my_sp] @RC OUTPUT, @id_=?, @pw=?, @depart=?, @class_=?, @name=?, @birthday=?, @grade=?, @subgrade=?;
    SELECT @RC AS rc;
    """
    values = (id_, pw, depart, class_, name, birthday, grade, subgrade)
    cursor.execute(sql, values)
    rc = cursor.fetchval()
    
    0 讨论(0)
  • 2020-12-30 00:55

    For MSSQL the correct format is this:

    SQL = 'exec sp_UpdateUserGoogleAuthenticated ''?'', ''?'''
    

    Try running the Stored Procedure in MSSQL in the SQL Query window and it will fail every time with () surrounding the ? marks. If you escape the single quotes it will allow for variables with spaces in them.

    0 讨论(0)
  • 2020-12-30 01:01

    Don't forget SET NOCOUNT ON in your stored procedure.

    0 讨论(0)
  • 2020-12-30 01:03

    With a cursor initialized by your connection, the sp can be called directly as follow

    sql = " exec your_SP @codemp = ?, @fecha = ? "
    prm = (dict['param1'], dict['param2'])
    cursor.execute(qry, params)
    
    0 讨论(0)
  • 2020-12-30 01:13

    From the pyodbc documentation

    To call a stored procedure right now, pass the call to the execute method using either a format your database recognizes or using the ODBC call escape format. (The ODBC driver will then reformat the call for you to match the given database.)

    For SQL Server you would use something like this:

    # SQL Server format
    cursor.execute("exec sp_dosomething(123, 'abc')")
    
    # ODBC format
    cursor.execute("{call sp_dosomething(123, 'abc')}")
    

    So to call your procedure

    id_ = 'test' 
    pw = '12345' 
    depart = 'none' 
    class_ = 'GM' 
    name = 'name' 
    birthday = 'None' 
    grade = 3 
    subgrade = 2 
    
    sql = 'exec [my_database].[dbo].[my_table](?, ?, ?, ?, ?, ?, ?, ?)'
    values = (id_, pw, depart, class_, name, birthday, grade, subgrade)
    
    cursor.execute(sql, (values))
    
    0 讨论(0)
提交回复
热议问题