Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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 3, 2009 1:51 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, December 15, 2015 8:04 AM
Points: 611, Visits: 351
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 3, 2009 2:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:52 PM
Points: 5,551, Visits: 10,727
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 3, 2009 4:11 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, December 15, 2015 8:04 AM
Points: 611, Visits: 351
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 3, 2009 4:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:52 PM
Points: 5,551, Visits: 10,727
Try to run this:

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

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, August 30, 2016 2:28 AM
Points: 3,034, Visits: 3,711
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 2016, 2014, 2012, 2008 R2, 2008 and 2005. 29 Aug 2016: now over 38,000 downloads.
Disclaimer: All information provided is a personal opinion that may not match reality.
Quote: "When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist." - Archbishop Hélder Câmara
Post #728911
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse