Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

@@Rowcount is not reliable? Expand / Collapse
Author
Message
Posted Wednesday, June 03, 2009 1:51 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, January 13, 2014 1:31 AM
Points: 606, Visits: 335
I have used @@RowCount next to one update statement in a stored procedure.
It was working fine in one of our production database, but recently we observed sometimes it will not work properly.

is @@RowCount is not reliable?

please advice.
Post #727944
Posted Wednesday, June 03, 2009 2:16 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Sunday, April 13, 2014 9:51 PM
Points: 4,570, Visits: 8,315
It never failed on my memory.
Unexpected results it returned only in cases when it was used incorrectly.

Can you show your code?
But don't modify anything, every detail may be very important.
Post #727954
Posted Wednesday, June 03, 2009 4:11 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Monday, January 13, 2014 1:31 AM
Points: 606, Visits: 335
Hi Sergiy,
my stored procedure is as follows..
basically this procedure should return @NewStatus only once if it is called multiple times for a given @ID

UPDATE Table SET Status=@NewStatus WHERE ID=@ID AND Status=@CurrentStatus
IF @@ROWCOUNT=1
RETURN @NewStatus
ELSE
RETURN @CurrentStatus

Post #728030
Posted Wednesday, June 03, 2009 4:24 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Sunday, April 13, 2014 9:51 PM
Points: 4,570, Visits: 8,315
Try to run this:

SELECT ID, Status, COUNT(*)
FROM Table
GROUP BY ID, Status
HAVING COUNT(*) > 1
Post #728036
Posted Thursday, June 04, 2009 7:09 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 3:21 AM
Points: 2,797, Visits: 3,079
You have to be careful when using @@ROWCOUNT. Your example should work as you expect, but the example below will not...

UPDATE .....
SELECT @MyErr = @@ERROR
IF @@ROWCOUNT > 1 ...

In this situation @@ROWCOUNT will always be 1 because of the SELECT running after the update that saves the error code.

If you need to save both the @@ERROR and @@ROWCOUNT values, use something similar to the code below. You can then refer to @MyErr and @MyCount whenever you want and always get the values from the UPDATE statement.
UPDATE...
SELECT @MyErr = @@ERROR, @MyCount = @@ROWCOUNT


Original author: SQL Server FineBuild 1-click install and best practice configuration of SQL Server 2014, 2012, 2008 R2, 2008 and 2005. 25 March 2014: now over 28,000 downloads.
Disclaimer: All information provided is a personal opinion that may not match reality.
Concept: "Pizza Apartheid" - the discrimination that separates those who earn enough in one day to buy a pizza if they want one, from those who can not.
Post #728911
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse