Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 113
sqlserver 94
BRH 78
#SQLServer 65
#TSQL 55
SQL Server 32
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2011 7
November 2007 7
November 2011 6
August 2011 6
October 2011 6
July 2011 6
September 2011 6
December 2011 6

Madhivanan's TSQL Blog

SQL Server - The power of @@rowcount, and the pit falls, if you do not use them correctly!

Aug 9 2010 1:32AM by Madhivanan   

Lot of newbies struggle to unserstand how @@Rowcount works in SQL Server. @@Rowcount is used to know the number of rows affected for the last select,insert,update or delete statements. You should use @@rowcount immediately after the statement

For example the following returns 1 row

select 100

To get row count, use @@rowcount immediately after the statemnet

select 100
select @@rowcount

It returns the values 100 and 1

Don't make any checkings directly on @@rowcount and assign it's value to a variable.

declare @row int
select 100
if @@rowcount>1
set @row=@@rowcount
--do some stuff

The above will return 0 becuase as soon as if @@rowcount>0 is executed it is reset to 0 as it doesn't return any rows. So always assign to variable first

declare @row int
select 100
set @row=@@rowcount
if @row>0
--do some stuff

Tags: t-sql, sql_server, tsql, BRH, #TSQL, @@rowcount,


Madhivanan
4 · 39% · 8850
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"SQL Server - The power of @@rowcount, and the pit falls, if you do not use them correctly!" rated 5 out of 5 by 1 readers
SQL Server - The power of @@rowcount, and the pit falls, if you do not use them correctly! , 5.0 out of 5 based on 1 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising