@@ROWCOUNT is it automatically reset to Zero?

I have some SQL in a stored procedure that updates a table row at a time. It does this using a while loop.
After each UPDATE performed in the loop, I need to determine if the UPDATE was succesfull. From what I know I must use the NOCOUNT and RowCount commands to determine if the update was successful. So
will my @ROWCOUNT value be reset to 0 after each loop iteration, or would I need to explicitly reset it?

Below is my sql, however it is more pseudocode that working sql at this time.

ALTER PROCEDURE YourProcName
            @Opmessage VARCHAR(100) OUTPUT
AS
DECLARE @i INT
DECLARE @UpperBound INT
SELECT @i = 0
SELECT @UpperBound = 10

WHILE @i < @UpperBound
BEGIN    SET NOCOUNT ON;
    UPDATE Table SET Table.Name ='Shaked' where Table.ID =@i1
    SET @I = @I + 1

END

IF (@@ROWCOUNT > 0)
BEGIN
    @Opmessage = 'Success!'
END
ELSE
BEGIN
   @Opmessage = 'Not success!'
END

END -- End of while loop
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
WHILE @i < @UpperBound
 BEGIN    SET NOCOUNT ON;
     UPDATE Table SET Table.Name ='Shaked' where Table.ID =@i1
 IF (@@ROWCOUNT > 0)
 BEGIN
     @Opmessage = 'Success!'
 END
 ELSE
 BEGIN
    @Opmessage = 'Not success!'
 END
     SET @I = @I + 1

 END
PortletPaulEE Topic AdvisorCommented:
the IF will reset @@rowcount to zero

Statements such as USE, SET <option>, DEALLOCATE CURSOR, CLOSE CURSOR, BEGIN TRANSACTION or COMMIT TRANSACTION reset the ROWCOUNT value to 0.
see: https://msdn.microsoft.com/en-AU/library/ms187316.aspx

IF falls into that bracket of commands ("such as")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
The only safe way to use @@ROWCOUNT is to immediately save it into your own local variable, then test against that:

...
DECLARE @UpperBound INT
DECLARE @rowcount int
 SELECT @i = 0
 SELECT @UpperBound = 10

 WHILE @i < @UpperBound
 BEGIN    SET NOCOUNT ON;
     UPDATE Table SET Table.Name ='Shaked' where Table.ID =@i1
     SET @rowcount = @@ROWCOUNT
     SET @I = @I + 1
...
IF (@rowcount > 0)
...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.