Demystifying @@ROWCOUNT: Your Guide to Tracking Deletions in Stored Procedures

2024-07-27

Counting Deleted Rows in a SQL Server Stored ProcedureMethods to Count Deleted Rows:

Here are two common approaches to count the number of deleted rows within a SQL Server stored procedure:

Using @@ROWCOUNT:

  • T-SQL provides a system variable named @@ROWCOUNT that stores the number of rows affected by the most recently executed Data Manipulation Language (DML) statement, including DELETE.
  • After the DELETE statement within your stored procedure, you can access the @@ROWCOUNT variable to retrieve the deleted row count.

Here's an example:

CREATE PROCEDURE DeleteCustomers 
(
  @CustomerID INT
)
AS
BEGIN
  DELETE FROM Customers WHERE CustomerID = @CustomerID;

  SELECT 'Number of deleted rows: ' + CAST(@@ROWCOUNT AS VARCHAR(10));
END;

In this example, the stored procedure DeleteCustomers takes a CustomerID as input and deletes the corresponding row from the Customers table. After the deletion, it retrieves the @@ROWCOUNT value and displays it as a message.

Using OUTPUT Clause:

  • The OUTPUT clause allows you to capture information from the deleted rows and return it as part of the result set.
  • You can use the DELETED.* alias to access all columns from the deleted rows and then use a COUNT(*) function to get the total count.
CREATE PROCEDURE DeleteCustomers 
(
  @CustomerID INT,
  @DeletedCount INT OUTPUT
)
AS
BEGIN
  DELETE FROM Customers 
  OUTPUT DELETED.* INTO @DeletedCount;

  SELECT 'Number of deleted rows: ' + CAST(@DeletedCount AS VARCHAR(10));
END;

This example is similar to the previous one, but it uses the OUTPUT clause to capture the deleted rows into an output parameter @DeletedCount. Then, it uses this parameter to display the count.

Choosing the Right Method:

  • If you only need the total number of deleted rows and don't need access to specific information from the deleted rows, @@ROWCOUNT is a simpler and more efficient option.
  • If you require details about the deleted rows (e.g., for logging purposes), the OUTPUT clause provides a more comprehensive solution.

Additional Considerations:

  • Remember that @@ROWCOUNT is a session-level variable and can be affected by other DML statements executed in the same session.
  • Be cautious when using @@ROWCOUNT within loops or transactions, as its value might not reflect the specific deletion you're interested in.

sql-server t-sql audit



SQL Server Locking Example with Transactions

Collision: If two users try to update the same record simultaneously, their changes might conflict.Solutions:Additional Techniques:...


Reordering Columns in SQL Server: Understanding the Limitations and Alternatives

Workarounds exist: There are ways to achieve a similar outcome, but they involve more steps:Workarounds exist: There are ways to achieve a similar outcome...


Unit Testing Persistence in SQL Server: Mocking vs. Database Testing Libraries

TDD (Test-Driven Development) is a software development approach where you write the test cases first, then write the minimum amount of code needed to make those tests pass...


Taming the Hash: Effective Techniques for Converting HashBytes to Human-Readable Format in SQL Server

In SQL Server, the HashBytes function generates a fixed-length hash value (a unique string) from a given input string.This hash value is often used for data integrity checks (verifying data hasn't been tampered with) or password storage (storing passwords securely without the original value)...


Understanding the Code Examples

Understanding the Problem:A delimited string is a string where individual items are separated by a specific character (delimiter). For example...



sql server t audit

Example Codes for Checking Changes in SQL Server Tables

This built-in feature tracks changes to specific tables. It records information about each modified row, including the type of change (insert


Bridging the Gap: Transferring Data Between SQL Server and MySQL

SSIS is a powerful tool for Extract, Transform, and Load (ETL) operations. It allows you to create a workflow to extract data from one source


Taming the Tide of Change: Version Control Strategies for Your SQL Server Database

Version control systems (VCS) like Subversion (SVN) are essential for managing changes to code. They track modifications


Can't Upgrade SQL Server 6.5 Directly? Here's How to Migrate Your Data

Outdated Technology: SQL Server 6.5 was released in 1998. Since then, there have been significant advancements in database technology and security


Replacing Records in SQL Server 2005: Alternative Approaches to MySQL REPLACE INTO

SQL Server 2005 doesn't have a direct equivalent to REPLACE INTO. You need to achieve similar behavior using a two-step process: