Support multi-statement execution with temp tables
维护者通常 2 天内回复
评估
这个 Issue 还没有评估数据。
描述
### Is your feature request related to a problem? Please describe.
Multi-statement SQL queries with temporary tables execute successfully but return empty result sets, while the same queries work correctly in SSMS(SQL Server Management Studio) and pyodbc. This occurs because each `cursor.execute()` call in mssql-python creates separate execution contexts, making local temp tables (`#temp`) created in one statement invisible in subsequent statements.
**Example that fails silently:**
```sql
-- Works in SSMS, returns empty results in mssql-python
SELECT col1, col2 INTO #temp FROM table1 WHERE condition
SELECT * FROM #temp JOIN table2 ON #temp.col1 = table2.col1
This prevents migration from legacy SQL Server applications and breaks existing complex reporting queries, forcing developers to completely rewrite working SQL.
Describe the solution you'd like
Add transparent temporary table support to the cursor.execute() method using pyodbc's proven approach:
- Auto-detection: Automatically identify multi-statement queries containing local temp tables
- SET NOCOUNT ON: Add
SET NOCOUNT ON;prefix to eliminate intermediate result sets that interfere with multi-statement execution - Batch execution: Execute the entire query as a single batch to preserve session scope for temp table visibility
- Zero API changes: Existing code works without modification
Benefits:
- Transparent - no API changes required
- Reliable - uses proven pyodbc approach
- Compatible - works with existing SQL without modification
- Minimal impact - adds approximately 20 lines of code
Describe alternatives you've considered
We evaluated and tested multiple approaches:
-
CTE Conversion (Tested - Works but Complex)
- Convert
SELECT ... INTO #tempstatements to Common Table Expressions (CTEs) - Replace temp table references with CTE names in subsequent queries
- ✅ Works: Successfully converts simple temp table patterns
- ❌ Complex: Requires extensive SQL parsing and transformation logic
- ❌ Maintenance burden: Custom conversion logic needs constant updates for edge cases
- ❌ Logic changes: CTEs have different scope and behavior than temp tables
- Convert
-
Statement Splitting (Tested - Partial Success)
- Split multi-statement SQL into individual statements
- Execute statements sequentially within the same transaction
- ✅ Works: Maintains temp table visibility between statements
- ❌ Session scope issues: Still suffers from mssql-python's execution context isolation
- ❌ Complex parsing: Difficult to reliably split complex SQL statements
- ❌ Parameter handling: Complications with parameter distribution across statements
-
Global Temp Tables (Works but Intrusive)
- Convert local temp tables (
#temp) to global temp tables (##temp) - ✅ Works: Global temp tables are visible across execution contexts
- ❌ Breaking changes: Requires modifying user SQL
- ❌ Cleanup complexity: Requires manual cleanup of global temp tables
- ❌ Concurrency issues: Global temp tables can conflict between parallel operations
- Convert local temp tables (
-
Custom Wrapper Methods (Works but Breaking)
- Add new methods like
execute_multistatement()orexecute_with_temp_tables() - ✅ Works: Can handle temp tables with specialized logic
- ❌ Breaking changes: Requires application code changes
- ❌ Adoption barrier: Developers must learn new API methods
- ❌ Inconsistent: Creates two different execution paths
- Add new methods like
-
SET NOCOUNT ON Approach (Chosen Solution)
- Add
SET NOCOUNT ON;prefix to problematic queries - Based on proven pyodbc implementation
- ✅ Works: Eliminates intermediate result sets that cause issues
- ✅ Simple: Minimal code changes required
- ✅ Proven: Already successfully used by pyodbc for same issue
- ✅ Non-intrusive: No API changes, works with existing code
- ✅ Reliable: Preserves original SQL structure and logic
- Add
Additional context
Research Sources:
- pyodbc Issue #993: https://github.com/mkleehammer/pyodbc/issues/993
- pyodbc Issue #263: https://github.com/mkleehammer/pyodbc/issues/263
- Stack Overflow: https://stackoverflow.com/questions/49516278/python-running-sql-query-with-temp-tables
Real-World Test Case:
This enhancement was validated with a complex 50+ line production query involving:
- Temp table creation with
SELECT ... INTO #TempEdi - Multiple JOINs across different databases
- Complex aggregation and filtering logic
- Previously failed silently (returned empty results)
- Now works correctly after applying SET NOCOUNT approach
Impact:
- Enables seamless migration from SSMS-based applications to Python
- Eliminates need for extensive query rewrites
- Brings mssql-python to feature parity with pyodbc for temp table handling
- Maintains backward compatibility while adding powerful new functionality
- 主要语言
- Python
- 星标
- 478
- 派生
- 62
- 平均合并
- 3 天 19 分钟
- 30 天内合并 PR
- 28
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
microsoft/mssql-python 的其他 Issue
-
area: bulk-copy triage needed
难度 2/5 1-3 小时 新手友好度 70/100
microsoft/mssql-python#843 · 1 条评论 ·
维护者通常 2 天内回复
-
triage needed
难度 4/5 3-5 天 新手友好度 35/100
microsoft/mssql-python#846 · 1 条评论 ·
维护者通常 2 天内回复
-
macOS: encrypted connections fail after Homebrew moved the `openssl` alias to `openssl@4`可能已有人在做 @bewithgaurav 于 15 天前认领。 未关闭area: packaging-platform bug triage needed
难度 4/5 3-5 天 新手友好度 55/100
microsoft/mssql-python#826 · 2 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
-
Announcement: mssql-python is coming to Django through mssql-django 2.0可能重新可做 @bewithgaurav 于 42 天前认领,目前没有进行中的 PR。 未关闭announcement
microsoft/mssql-python#748 · 已指派 1 人 ·
维护者通常 2 天内回复
-
Connection pool can briefly exceed max_size when pooling is disabled under load可能已有人在做 @bewithgaurav 于 24 天前认领。 未关闭area: connectivity-auth bug triage done under development
难度 4/5 3-5 天 新手友好度 62/100
microsoft/mssql-python#746 · 1 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
查看 microsoft/mssql-python 的全部 Issue
相似的 Issue
-
Workflow.schedule stops when a cron deadline passes before sleep可能已有人在做 @HuaTNA 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
[Bug] HiveDialect shares its colspecs dict with SQLAlchemy's DefaultDialect (python/pyhive)可能已有人在做 @rusackas 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 80/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 80/100
维护者通常 1 天内回复
-
[20.0] hr_work_entry: Duration column of Export Time Entries is truncated to whole hours可能已有人在做 @odooaktiv 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
fix formatting of the sample database page of the docs可能已有人在做 @Aaqibhafeezkhan 今天认领。 未关闭documentation good first issue help wanted
难度 1/5 1 小时以内 新手友好度 72/100
chaoss/CollectOSS#544 · 1 条评论 ·
维护者通常 1 天内回复