Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Records Affected for Stored Procedure 1

Status
Not open for further replies.

andrea96

Programmer
Jan 26, 2001
201
US
Is there a way to get the number of records affected for each statement in a stored procedure, not the total number of records affected by the stored procedure that I would get using RecordsAffected? For example, I have a stored procedure that performs a delete and then an insert, and I need the number of records deleted and the number of records inserted.

Thanks,
Andrea
 
You will have to use @@ROWCOUNT in your Stored Procedure
Something like below should work, although it takes no consideration for any triggers which may fire and perform cascade deletes

Code:
DECLARE @iInserted INT
DECLARE @iDelete INT

DELETE FROM MyTable WHERE.....
SET @iDeleted = @@ROWCOUNT

INSERT INTO MyTable............
SET @iInserted = @@ROWCOUNT

select @iInserted as Inserted, @iDeleted as Deleted

This will also give you a table back with the counts required. Im sure you can grasp the concepts and amend as you need to.




Sweep
...if it works dont mess with it
 
Thanks. I had decided to use output parameters with SELECT COUNT(*), but @@ROWCOUNT is a better solution.
 
You're welcome...glad to be of some help.

Sweep
...if it works dont mess with it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top