Skip to content

Retrieving records affected by statement #17445

@roji

Description

@roji

DbDataRecord has a RecordsAffected property, which allows users to know how many rows were inserted/updated/deleted. However, when executing a multi-statement command (e.g. UPDATE xxx; UPDATE xxx), this property provides an aggregation of all rows across statements. There's no programmatic way to get records affected per statement.

For an example of why this might be useful, see EFCore's modification command batch. One of EFCore's major new features are command batching (i.e. the use of multi-statement commands), but in order to implement optimistic concurrency it needs to know, for each command, how many records were affected. For SqlServer this is implemented by sending SELECT @@ROWCOUNT after each update, and parsing the results.

PostgreSQL has no equivalent SQL query, so in Npgsql I actually developed a statement-by-statement RecordsAffected mechanism to support this. In a nutshell, a collection of NpgsqlStatements is exposed by NpgsqlDataReader, and each object has its own RecordsAffected. Let me know if you want more detail.

Note that this is somewhat related to #15375, where the possibility of a better API for command batching is discussed (the latter may/could also depend on multiple DbStatement instances being managed within a single DbCommand).

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Datahelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions