SQL Server @@ROWCOUNT is returning always zero in stored procedure [duplicate]

j2datikz  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(111)

This question already has an answer here:

Why is the @@ROWCOUNT variable returning zero after the IF statement (1 answer)
Closed 23 days ago.

Here is my stored procedure

/****** Object:  StoredProcedure [dbo].[TestStoProc]    Script Date: 21.09.2023 13:32:40 ******/ 
    ALTER PROCEDURE [dbo].[PurgeSyncOperations] 
       -- Parameters for the stored procedure here
         @Date datetime, 
         @CustomerNumber nvarchar(10),
         @count int = 0 output
    AS
        BEGIN
            DECLARE @row INT;
            SET @row = 1;

            WHILE @row > 0
                BEGIN
                -- Statements for archiving and deletion

                    //some query to insert data

                    //some query to delete data

                    SET @row = @@ROWCOUNT;
                    SET @count = @count + @@ROWCOUNT
                END
        END

I am getting always zero from this stored procedure. Need help on this.

ybzsozfc

ybzsozfc1#

SET @row = @@ROWCOUNT;
SET @count = @count + @@ROWCOUNT

assigns rowcount from the set @row = @@rowcount operation.

Surely you want:

SET @row = @@ROWCOUNT;
SET @count = @count + @row

相关问题