Jeremy
Jeremy

Reputation: 9467

Pymssql: Calling Stored Procedure with parameters

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'))

Upvotes: 3

Views: 17627

Answers (2)

Alex
Alex

Reputation: 5646

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.

Upvotes: 9

Jeremy
Jeremy

Reputation: 9467

I found the correct syntax to do this:

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

Upvotes: 3

Related Questions