The usage of Rowcount and @@Rowcount in SQL Server.

In SQL Server, ROWCOUNT and @@ROWCOUNT are two different concepts and functions.

  1. ROWCOUNT is a system variable in SQL Server that stores the number of rows affected by the last executed statement. It can be set using the SET ROWCOUNT statement or retrieved by querying the system variable. Here is an example of how ROWCOUNT is used:
  2. Set the value of ROWCOUNT to 10: SET ROWCOUNT 10; – Set ROWCOUNT as 10.
  3. Check the value of ROWCOUNT: SELECT @@ROWCOUNT; – Retrieve the number of rows affected by the last executed statement.
  4. ROWCOUNT is mainly used to restrict or control the number of rows returned in a query. By setting the value of ROWCOUNT, only the specified number of rows will be returned when executing the query.
  5. @@ROWCOUNT is a system function that returns the number of rows affected by the last executed statement. It is mainly used to determine the number of rows affected after executing a SQL statement. Here is an example of how to use @@ROWCOUNT:
  6. Retrieve the value of @@ROWCOUNT: SELECT @@ROWCOUNT; – Obtain the number of rows affected by the last executed statement.
  7. @@ROWCOUNT can be used to determine the number of rows affected after the execution of the previous SQL statement, and based on the returned number of rows, appropriate processing logic can be applied.

It’s important to note that there is a difference between the values of ROWCOUNT and @@ROWCOUNT. ROWCOUNT is a variable that stores the number of rows affected by the last executed statement, while @@ROWCOUNT is a function that returns the number of rows affected by the last executed statement.

bannerAds