You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[QUESTION] Are multiple active result sets supported? #395
area: api-compliancePython API behavior and typing: DB-API 2.0, exceptions, type stubs, new APIs.enhancementNew feature or requestquestionFurther information is requested
Are multiple active result sets supported, i.e. can I have multiple active cursors on the same connection? In pyodbc, for example, this is supported via the connection property MultipleActiveResultSets=True.
Thanks for the feedback Gaurav Sharma (@bewithgaurav). Do you see this as something that will eventually be supported and would it therefore make sense to open a feature request for it?
Do you see this as something that will eventually be supported and would it therefore make sense to open a feature request for it?
You already have opened a feature request. This issue. 😃
That said, what's your use case / the usage pattern you have specifically in Python that requires MARS? We know how it can be used. It just helps to hear the real-world scenarios. Your answer, and upvotes on this issue, will help us prioritize when to bring the feature into mssql-python.
Martin Studer (@martinstuder) What you've described doesn't require MARS. MARS is all about interleaving the fetching of results of multiple queries on a single connection. For example, in a parent/child relationship you might query parent records and as you are reading through the parent result set, you might query child records for each parent. (Note: If testing the effect of MARS, ensure parent result is relatively large (> packet size) to ensure the driver isn't just getting all the results in a single response from the server.)
You could also be using MARS to cover poor practices like leaving results/cursors open before moving on to the next query against the connection. This isn't really a valid use case. You would eventually run into too many open cursors. But it would "work" for relatively short-lived scripts.
David Engel (@David-Engel) As a result of sharing a single connection we do end up in situations where we interleave the fetching of results of multiple queries. But I think we could adapt our connection management in a way that MARS would no longer be needed. So maybe there is a certain element of "poor practices" that you were mentioning ...
Martin Studer (@martinstuder) Are you sure pyodbc allows interleaved results? I'm unable to get it working on this end. Maybe I'm doing something wrong?
conn = pyodbc.connect("Driver={ODBC Driver 18 for SQL Server};Server=myserver;Database=mydb;Uid=myuid;Pwd=mypwd;Encrypt=No;MultipleActiveResultSets=True")
cur = conn.cursor()
x = cur.execute('select * from generate_series(0, 1000000)')
print(x.fetchone())
# (0,)
y = cur.execute('select * from generate_series(1000001, 9999999)')
print(y.fetchone())
# (1000001,)
print(x.fetchone())
# (1000002,)
David Engel (@David-Engel) Thanks, that did the trick. I'm extremely surprised to learn that pyodbc supports that. Always seemed like a useless feature to me 🤷
area: api-compliancePython API behavior and typing: DB-API 2.0, exceptions, type stubs, new APIs.enhancementNew feature or requestquestionFurther information is requested
Activity
bewithgaurav commentedon Jan 7, 2026
Hi Martin Studer (@martinstuder)
We don't support
MultipleActiveResultSetsas of now.martinstuder commentedon Jan 12, 2026
Thanks for the feedback Gaurav Sharma (@bewithgaurav). Do you see this as something that will eventually be supported and would it therefore make sense to open a feature request for it?
I guess the current workaround is to use separate connections: https://github.com/microsoft/mssql-python/wiki/Database-Transaction-Management#concurrent-database-transactions
David-Engel commentedon Jan 14, 2026
You already have opened a feature request. This issue. 😃
That said, what's your use case / the usage pattern you have specifically in Python that requires MARS? We know how it can be used. It just helps to hear the real-world scenarios. Your answer, and upvotes on this issue, will help us prioritize when to bring the feature into mssql-python.
martinstuder commentedon Jan 19, 2026
Our use case is that we typically create a single connection (using
DefaultAzureCredentialas described at https://learn.microsoft.com/en-us/azure/azure-sql/database/azure-sql-python-quickstart?view=azuresql&tabs=windows%2Csql-inter#add-code-to-connect-to-azure-sql-database) and cache that connection for as long as possible . If connection pooling inmssql_pythonworks with a constantly changing access token we could probably switch to an approach where we always create a new connection.David-Engel commentedon Jan 20, 2026
Martin Studer (@martinstuder) What you've described doesn't require MARS. MARS is all about interleaving the fetching of results of multiple queries on a single connection. For example, in a parent/child relationship you might query parent records and as you are reading through the parent result set, you might query child records for each parent. (Note: If testing the effect of MARS, ensure parent result is relatively large (> packet size) to ensure the driver isn't just getting all the results in a single response from the server.)
You could also be using MARS to cover poor practices like leaving results/cursors open before moving on to the next query against the connection. This isn't really a valid use case. You would eventually run into too many open cursors. But it would "work" for relatively short-lived scripts.
martinstuder commentedon Jan 26, 2026
David Engel (@David-Engel) As a result of sharing a single connection we do end up in situations where we interleave the fetching of results of multiple queries. But I think we could adapt our connection management in a way that MARS would no longer be needed. So maybe there is a certain element of "poor practices" that you were mentioning ...
amachanic commentedon Jan 29, 2026
Martin Studer (@martinstuder) Are you sure pyodbc allows interleaved results? I'm unable to get it working on this end. Maybe I'm doing something wrong?
David-Engel commentedon Jan 30, 2026
Adam Machanic (@amachanic) Two problems. (1) You need to create a new cursor. (2) The ODBC option to enable MARS is
MARS_Connection=Yes.cur = conn.cursor()
x = cur.execute('...') # x is cur
y = cur.execute('...') # y is the SAME cur, which cancels x's result
amachanic commentedon Jan 30, 2026
David Engel (@David-Engel) Thanks, that did the trick. I'm extremely surprised to learn that pyodbc supports that. Always seemed like a useless feature to me 🤷