• Question

  • Hi, all

    I was reading this article @@ROWCOUNT (Transact-SQL) (http://technet.microsoft.com/en-us/library/ms187316.aspx).

    To me, @@ROWCOUNT is like a global variable that modified by the last executed statement. Do I need to provide some locking mechanism to create a critical section when accessing the value of @@ROWCOUNT? If so, coule you help me with an example of the locking mechanism?

    E.g. in the example of the above link

    USE AdventureWorks;
    UPDATE HumanResources.Employee 
    SET Title = N'Executive'
    WHERE NationalIDNumber = 123456789
    IF @@ROWCOUNT = 0
    PRINT 'Warning: No rows were updated';
    I wonder whether there is a chance before IF @@ROWCOUNT = 0, some other user executes another statement which sets @@ROWCOUNT to another value so that if there is no locking mechanism, the result would be wrong.

    Thank you very much!

    Tuesday, December 29, 2009 8:56 PM


  • @@ROWCOUNT's value is only pertinent to your current session... it is not affected by other queries going on in other sessions.

    Just make sure you check its value immediately after the query you're checking (i.e. no intervening queries/statements that could also affect its value).

    --Brad (My Blog)
    • Marked as answer by Might Tuesday, December 29, 2009 9:52 PM
    Tuesday, December 29, 2009 9:09 PM