Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get number of deleted records?

Tags:

sql-server

I have a stored procedure which deletes certain records. I need to get the number of deleted records.

I tried to do it like this:

DELETE FROM OperationsV1.dbo.Files WHERE FileID = @FileID    SELECT @@ROWCOUNT AS DELETED;             

But DELETED is shown as 0, though the appropriate records are deleted. I tried SET NOCOUNT OFF; without success. Could you please help? Thanks.

like image 642
David Shochet Avatar asked Dec 20 '11 15:12

David Shochet


People also ask

How do I count a deleted record in SQL?

Files WHERE FileID = @FileID SELECT @@ROWCOUNT AS DELETED; But DELETED is shown as 0, though the appropriate records are deleted.

How do I count a deleted record in Oracle?

SELECT COUNT(*) FROM suppliers( DELETE from supplier( WHERE EXISTS ( SELECT customers. customer_name FROM customers WHERE customers.

How can I count deleted records in mysql?

You can use ROW_COUNT() function to check the number of deleted rows. The conditions in the WHERE clause (optional) identify which rows to delete. Without WHERE clause, all rows are deleted. If you specify the ORDER BY clause, the rows are deleted in specified order.

How do you find who deleted records in SQL Server?

Right click SQL Server Instance and Select Reports -> Standard Reports -> Schema Changes History as shown in the below snippet. 3. This will open up Scheme Changes History report which will have the details about who deleted the SQL Server Database along with the timestamp when the database was deleted.


2 Answers

That should work fine. The setting of NOCOUNT is irrelevant. This only affects the n rows affected information sent back to the client and has no effect on the workings of @@ROWCOUNT.

Do you have any statements between the two that you have shown? @@ROWCOUNT is reset after every statement so you must retrieve the value immediately with no intervening statements.

like image 87
Martin Smith Avatar answered Oct 11 '22 10:10

Martin Smith


I use this code snippet when debugging stored procedures to verify counts after operations, such as a delete:

DECLARE @Msg varchar(30) ... SELECT @Msg = CAST(@@ROWCOUNT AS VARCHAR(10)) + ' rows affected' RAISERROR (@Msg, 0, 1) WITH NOWAIT 
like image 37
James Drinkard Avatar answered Oct 11 '22 09:10

James Drinkard