cursor execute command prematurely exits without error for long running stored procedure?
Please first make sure you have looked at:
- Documentation: https://github.com/mkleehammer/pyodbc/wiki
- Other issues
Environment
To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit:
- Python: Python 3.6.8
- pyodbc: pyodbc 4.0.28
- OS: centOS 7
- DB: MSSQL 2012
- driver: msodbcsql17
Issue
Often it is easiest to describe your issue as "expected behavior" and "observed behavior".
Have a pyodbc script that is supposed to execute a stored procedure on an MSSQL server, but appears to be cutting out without error in the middle of running.
I have pyodbc that connects to a MSSQL (2012) server runs a stored procedure (that runs several other subordinate stored procedures) which normally lasts for about 45min, but when running via pyodbc it appears to exit after a certain amount of time (though never exactly consistent in how long) where it just quits without warning or error (does not occur when running manually on the MSSQL server itself, thus why I think having "SET ANSI_WARNINGS OFF" is fine). Adding debugging print statements to the subordinate stored procedures seems to confirm this: execution seems to simply end in the middle of running (usually after about 25-26min). The code looks like...
cnxn = pyodbc.connect(f"DSN={CONFS['odbc_dsn']};"
f"DATABASE={'mydb'};"
f"UID={CONFS['username']};PWD={CONFS['password']};"
f"MultipleActiveResultSets=True;",
autocommit=True)
cursor = cnxn.cursor()
print("\n\n\nRunning web reporting processes...")
stored_procs = [
"mydb..some_initialization_stuff",
"mydb..long_running_stored_proc"
]
cursor.commit()
for sp in stored_procs:
print(f"\n\t[{datetime.datetime.now()}] Running stored procedure {sp}")
cursor.execute(f"SET ANSI_WARNINGS OFF; exec {sp}")
# cursor.commit()
print(f"\t[{datetime.datetime.now()}] stored procedure {sp} completed")
# print(cursor.fetchall())
cursor.close()
cnxn.close()
Anyone with more experience with pyodbc know what could be causing this? Any other information / specific debugging steps to improve this question?
After further debugging, found that using
cursor.execute(f"SET NOCOUNT ON; exec {sp}")
does not run into the same problem. Not quite sure why this would be. Any ideas as to why this works?
You need to read generated resultsets from the server, otherwise you will block it from further execution once it fills its output buffer. NOCOUNT prevents row counts from being returned.
See http://support.microsoft.com/kb/827575 for more information.
Interesting, thanks
Thank you for the set nocount on answer.
Instead of cursor.execute(f"SET NOCOUNT ON; exec {sp}") you could add "set nocount on;" to the stored procedure itself. Thank you again for answering this for me!
Hi, SET NOCOUNT ON was working for me, when I was using SQLAlchamey cursor explicitly,, but it stopped working for me, when I switched to SQLAlchamey sessions.
Any idea on how to fix this?
You can compare the ODBC traces to see the difference it makes.