Using SQL Server stored procedures from Python (pyodbc)

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' 
class_ = 'GM' 
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))

Leave a Comment