Cursor.nextset() does not update rowcount for the next result
Maintainer thường phản hồi trong vòng 2 ngày
@jahnvi480 đang làm issue này rồi.
Từ ngày 28/10/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
Hướng nghiên cứu
Bắt đầu với Cursor_nextset và phần triển khai execute() để so sánh cách mỗi phần cập nhật rowcount; issue nêu tên các điểm vào này nhưng không nêu đường dẫn tệp. Sau đó, đọc các kiểm thử cursor hiện có trong tests/test_004_cursor.py và bổ sung kiểm thử cho số hàng bị ảnh hưởng liên tiếp và một tập kết quả. Hoàn tất khi rowcount phản ánh từng kết quả sau nextset() và là -1 đối với một tập kết quả.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
After Cursor.nextset() moves to the next result of a multi-statement batch, Cursor.rowcount still holds the value set by execute() for the first statement. The affected-row count of every later INSERT/UPDATE/DELETE in the batch therefore cannot be read through the DB API.
nextset() calls SQLMoreResults and refreshes description and messages, but it never calls SQLRowCount, unlike execute(). pyodbc refreshes rowcount there (Cursor_nextset calls SQLRowCount after SQLMoreResults), and nextset()'s docstring cites pyodbc parity.
This matters most for T-SQL batches with control flow. A WHILE loop that inserts rows sends one result per INSERT, and the only way to count the rows the batch changed is to read rowcount after each nextset(). Tools that report "N rows affected" per batch (migration runners, deployment CLIs) under-report, and a loop that inserted 5 rows shows 0 or 1.
No exception is raised.
To reproduce
import mssql_python
from mssql_python import ddbc_bindings
conn = mssql_python.connect(
"Server=localhost,1433;Database=tempdb;UID=sa;PWD=<password>;"
"Encrypt=yes;TrustServerCertificate=yes;"
)
cur = conn.cursor()
cur.execute("CREATE TABLE #t (id INT)")
cur.execute("""
INSERT INTO #t VALUES (1);
INSERT INTO #t VALUES (2), (3);
UPDATE #t SET id = id + 10;
""")
seen = [cur.rowcount]
native = [ddbc_bindings.DDBCSQLRowCount(cur.hstmt)]
while cur.nextset():
seen.append(cur.rowcount)
native.append(ddbc_bindings.DDBCSQLRowCount(cur.hstmt))
print("cursor.rowcount:", seen) # [1, 1, 1]
print("SQLRowCount: ", native) # [1, 2, 3]
With a loop, every INSERT of the WHILE is its own result:
cur.execute("""
DECLARE @i INT = 1;
WHILE @i <= 3
BEGIN
INSERT INTO #t SELECT TOP (@i) n FROM (VALUES (1), (2), (3)) AS v(n);
SET @i = @i + 1;
END;
""")
counts = [cur.rowcount]
while cur.nextset():
counts.append(cur.rowcount)
print(counts) # [1, 1, 1] -- expected [1, 2, 3]
Expected behavior
After each nextset() that returns True, rowcount reflects the result the cursor is now on, the same way it does after execute():
- the affected-row count for an INSERT/UPDATE/DELETE/MERGE result;
-1for a result set, whichfetch*()then updates as it does today.
For the first example that is [1, 2, 3]. This matches pyodbc on the same batch.
Further technical details
- Python version: 3.12
- mssql-python: 1.16.0 (also seen on 1.4.0; the code on
mainat bd0aaf89 is the same) - SQL Server version: SQL Server 2022 (16.0.4305.5, RTM-CU27-GDR)
- Operating system: Debian 12 (bookworm), in a container
Additional context
A fix is ready, and I can open a PR for it. After the describe step, nextset() would set rowcount the same way execute() does: -1 when the new result has columns, otherwise DDBCSQLRowCount(self.hstmt). It comes with two regression tests in tests/test_004_cursor.py.
I did not look at AsyncCursor.nextset(), which goes through py-core and may need its own check.
- Ngôn ngữ chính
- Python
- Star
- 481
- Fork
- 62
- Merge trung bình
- 2 ngày 14 giờ
- Pull request đã merge (30 ngày)
- 14
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/mssql-python
-
bulkcopy() silently truncates fractional seconds in string datetime values to microsecondsCó thể đã có người làm @jahnvi480 đã nhận 1 ngày trước. Đang mởarea: bulk-copy bug inADO triage done under development
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
microsoft/mssql-python#869 · 2 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Make azure-identity an optional dependencyCó thể đã có người làm @jahnvi480 đã nhận 1 ngày trước. Đang mởarea: connectivity-auth dependencies enhancement triage needed
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
microsoft/mssql-python#862 · 3 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
triage needed
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 38/100
microsoft/mssql-python#850 · 2 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
bulkcopy() & bulkcopy_arrow() fail with Schannel AcquireCredentialsHandleW error 0x8009030D (SEC_E_UNKNOWN_CREDENTIALS)Có thể đã có người làm @jahnvi480 đã nhận 9 ngày trước. Đang mởarea: bulk-copy bug inADO triage done
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
microsoft/mssql-python#848 · 5 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
macOS: encrypted connections fail after Homebrew moved the `openssl` alias to `openssl@4`Có thể làm lại được @bewithgaurav đã nhận 29 ngày trước và không có pull request nào đang mở. Đang mởarea: packaging-platform bug triage needed
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
microsoft/mssql-python#826 · 2 bình luận · 4 reaction · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của microsoft/mssql-python
Issue tương tự
-
app: scripts status: backlog type: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
netbox-community/netbox-scripts#68 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
marin-community/marin#10765 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
flathub/com.unity.UnityHub#230 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Claiming namespace `lang2hong`Đang mởnamespace operations
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 63/100
EclipseFdn/open-vsx.org#14910 ·
Maintainer thường phản hồi trong vòng 1 ngày