Question on @@ROWCOUNT

Mikhail Firsov 1,881 Reputation points
2021-04-19T14:30:35.283+00:00

Hello,

Would you please help me with this?

89162-q.png

UPDATE dbo.UsersCOMPUTERS2  
SET LastName = N'Test2'  
WHERE LastName = N'Test'  
IF @@ROWCOUNT = 0  
PRINT 'Warning! No rows were updated!!!'  
ELSE PRINT @@ROWCOUNT;  
go  
Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Tom Phillips 17,771 Reputation points
    2021-04-19T17:06:35.937+00:00

    The documentation is not very clear on @@ROWCOUNT.

    You should treat @@ROWCOUNT as a "read-once" variable. Anytime you use @@ROWCOUNT, it gets reset to 0. So your statement "IF @@ROWCOUNT" resets the value to 0. You need to store the value in a local variable if you want to reuse it.

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2021-04-19T15:03:05.443+00:00

    Hi @Mikhail Firsov ,

    You need to assign the @@ROWCOUNT to a T-SQL variable first to preserve its value.
    Please see a repro of it below.

    SQL

    -- DDL and sample data population, start  
    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, city VARCHAR(30));  
    INSERT INTO @tbl (city) VALUES  
    ('Miami'),  
    ('Orlando');  
      
    DECLARE @counter INT;  
    -- DDL and sample data population, end  
      
    UPDATE @tbl  
    SET city = 'Hollywood'  
    WHERE ID = 1;  
      
    SET @counter = @@ROWCOUNT;  
      
    IF @counter = 0  
     PRINT 'Warning! No rows were updated!!!';  
    ELSE   
     PRINT @counter;  
    
    1 person found this answer helpful.
    0 comments No comments

  2. SQLZealots 276 Reputation points
    2021-04-19T14:56:36.627+00:00

    Try the below: The IF is resetting the value of @@rowcount in your case.

    Declare @RCnt bigint
    UPDATE dbo.T1
     SET Col1=3
     WHERE col1=2
     Set @RCnt = @@ROWCOUNT
     IF @RCnt = 0
     PRINT 'Warning! No rows were updated!!!'
     ELSE PRINT @RCnt;
    
    0 comments No comments

  3. Mikhail Firsov 1,881 Reputation points
    2021-04-19T15:43:29.6+00:00

    Thank you all for your replies!

    "The IF is resetting the value of @@rowcount in your case. " - ??? If the "IF" was resetting the @@ROWCOUNT that query would always print "Warning! No rows were updated!!!'", but it prints it only when there was no rows indeed...

    I was able to create a query with the variable, I just wanted to know whether I understand it correctly why the @@ROWCOUNT was 0 in the code above.

    According to this page - https://learn.microsoft.com/en-us/sql/t-sql/functions/rowcount-transact-sql?view=sql-server-ver15 - the PRINT operator must set it to 0, SELECT - to 1 ( but I always kept getting 0 after SELECT - don't know why) and that was the cause of the "0".


  4. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-04-19T21:43:48.657+00:00

    To add to the other posts: @@rowcount returns the number of rows affected by the most recently executed statement. When you come to the PRINT, the most recently executed statement is the IF.

    For this reason, you should always capture @@rowcount in a local variable if you want to play with it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.