Calling MSSQL stored procedure from SqlAlchemy
I remember this giving me grief before too. From memory either session.execute()
or connection.execute()
worked for me. There's also a callproc()
method, which is probably the right way to go. http://docs.sqlalchemy.org/en/latest/core/connections.html
Also, I've had issues in the past with MSSQL which seemed to be due to something asynchronous happening where the method was returning before the procedure was finished, which resulted in unpredictable results on the database. I found that putting a time.sleep(1)
(or whatever the appropriate number is) right after the call fixed this.
To have it working in sqlalchemy I managed to do it this way:
from sqlalchemy import create_engine engine = create_engine('mssql+pyodbc://usr:passw@srv/db?driver=SQL Server', echo=True) with engine.begin() as conn: conn.execute("exec dbo.your_proc")