Get the number of records affected by a stored procedure.

For INSERT, UPDATE and DELETE SQL statements executed directly against the database, most database providers return the count of rows affected. For stored procedures, the number of records affected is always -1.

How do we get the number of records affected by a stored procedure?

Register an out parameter for the stored procedure, and set the value based on @@ROWCOUNT if using SQL Server.

Mind that if you have multiple INSERT/UPDATE/DELETEs, you’ll need a variable to store the result from @@ROWCOUNT for each operation.
Turn out SET NO COUNT ON in the stored procedure script (by default on Sql-Server Management Studio) and SqlCommand.ExecuteNonQuery(); always returned -1.

Just set it off : SET NO COUNT OFF without needing to use @@ROWCOUNT.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s