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.
Files WHERE FileID = @FileID SELECT @@ROWCOUNT AS DELETED; But DELETED is shown as 0, though the appropriate records are deleted.
SELECT COUNT(*) FROM suppliers( DELETE from supplier( WHERE EXISTS ( SELECT customers. customer_name FROM customers WHERE customers.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With