Wednesday, May 7, 2008

Updated Rowcount on SQL Server

This was a usual day today in office and i was working on a requirement in which i was needed to fetch the total number of rows effected by an update query, so I asked my best code mate "Google" and to my surprise there was not enough correct answers at least the one i was looking for.

There were suggestions that you can use a select statement for the updated rows and make it like a select (count) which works fine, but just looking into the SQL server books online, it shows that there is even a better way to do it.

After the update statement in my stored procedure i used "@@ROWCOUNT" with a select statement and it works like a charm.

so the little find for my first ever technical blog is that there is a better way to find the total updated rows by a query

Example:
DB: Northwind , Table Employees

update employees set extension='1234'
select @@ROWCOUNT

This will return 9 (default rows in this table) as the rows effected

Hope this helps