SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


@@Rowcount is not reliable?


@@Rowcount is not reliable?

Author
Message
Raghavendra-499237
Raghavendra-499237
SSChasing Mays
SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)

Group: General Forum Members
Points: 637 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.
Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10826 Visits: 11970
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.
Raghavendra-499237
Raghavendra-499237
SSChasing Mays
SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)SSChasing Mays (637 reputation)

Group: General Forum Members
Points: 637 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
Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10826 Visits: 11970
Try to run this:

SELECT ID, Status, COUNT(*)
FROM Table
GROUP BY ID, Status
HAVING COUNT(*) > 1
EdVassie
EdVassie
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6032 Visits: 3866
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 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005. 14 Mar 2017: now over 40,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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search