Scope of @@rowcount?

后端 未结 2 1025
北海茫月
北海茫月 2020-12-30 21:06

What is the scope of @@rowcount ?MSDN doesn\'t mention its scope.

Returns the number of rows affected by the last statement.

2条回答
  •  清歌不尽
    2020-12-30 21:51

    every insert/update/select/set/delete statement resets the @@rowcount to the rows affected by the executed statement

    begin
    declare @myrowcount int,
        @myrowcount2 int
    insert stmt
    SET @myrowcount=@@rowcount
    if @myrowcount>0
    begin
    insert stmt
    SET @myrowcount2 =@@rowcount
    if @myrowcount2 >0
    do smthg
    end
    end
    

    or try this

     SELECT * FROM master.sys.objects -- 50 rows
        IF (1=1)
            SELECT @@ROWCOUNT AS RowsAffected -- 0, because the IF did not affect any rows
    

    even an IF statement affects it....hence its scope is the last statement read.

提交回复
热议问题