Pymssql: Calling Stored Procedure with parameters

12,532

Solution 1

I found the correct syntax to do this:

connection.execute_query('exec storedProcedureName @Id=1')

Solution 2

For anyone using a newer version of pymssql, you can call procedures with parameters like this: cursor.callproc('storedProcedureName', (1,))

Where the second parameter is a tuple of all the parameters required.

Share:
12,532
Jeremy
Author by

Jeremy

Updated on June 05, 2022

Comments

  • Jeremy
    Jeremy almost 2 years

    I am using pymssql to make database calls to a SQL 2005 database. I want to pass parameters to a stored procedure. I am having a hard time getting the correct syntax for that. Here is what I have for calling a procedure with no parameters:

    import _mssql
    connection = _mssql.connect(server='myserver', database='mydatabase', trusted=True)
    connection.execute_query('storedProcedureName')
    

    Suppose I wanted to set the @Id parameter to 1. How would I do that? The following lines do not work. The docs are unclear as to how the params are structured.

    connection.execute_query('storedProcedureName', {'@Id':'1'})
    connection.execute_query('storedProcedureName', '@Id=1')
    connection.execute_query('storedProcedureName', ('@Id', '1'))