I have a python script that uses pyodbc to call an MSSQL stored procedure, like so:
cursor.execute(\"exec MyProcedure @param1 = \'\" + myparam + \"\'\")
I know this is old, but I just spent several hours trying to figure out how to make my Python code wait for a stored proc on MSSQL to finish.
The issue is not with asynchronous calls.
The key to resolving this issue is to make sure that your procedure does not return any messages until it's finished running. Otherwise, PYDOBC interprets the first message from the proc as the end of it.
Run your procedure with SET NOCOUNT ON
. Also, make sure any PRINT
statements or RAISERROR
you might use for debugging are muted.
Add a BIT parameter like @muted
to your proc and only raise your debugging messages if it's 0
.
In my particular case, I'm executing a proc to process a loaded table and my application was exiting and closing the cursor before the procedure finished running because I was getting row counts and debugging messages.
So to summarize, do something along the lines of
cursor.execute('SET NOCOUNT ON; EXEC schema.proc @muted = 1')
and PYODBC will wait for the proc to finish.