Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Cursor.nextset() does not update rowcount for the next result

Đã đóng Phù hợp với người mới
#863 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, sql
Lĩnh vực
backend, database

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ả

triage needed
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;
  • -1 for a result set, which fetch*() 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 main at 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

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/mssql-python

Tất cả issue của microsoft/mssql-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.