How can I get the number of records affected by a stored procedure?

dthrasher picture dthrasher · Jul 29, 2009 · Viewed 155.5k times · Source

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?

Answer

OMG Ponies picture OMG Ponies · Jul 29, 2009

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

Mind that if you have multiple INSERT/UPDATE/DELETE, you'll need a variable to store the result from @@ROWCOUNT for each operation.