views:

107

answers:

1

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

I found the correct syntax to do this:

connection.execute_query('exec storedProcedureName @Id=1')
Jeremy
Be very careful to protect yourself from SQL injections. I'm not in a place to test but it looks like what you want is something like connection.execute_query('exec storedProcedureName @Id=%d',(1,))
Mark Peters
Thanks for the tip. I wanted to make my example as simple as possible.
Jeremy