Добавьте еще одну переменную для суммирования @@rowcount
- вам может понадобиться использовать bigint
, а не int
.
DECLARE @rc INT = 1, @total bigint = 0;
WHILE @rc > 0
BEGIN
BEGIN TRANSACTION;
INSERT INTO MyTargetTable (Id, SendId, RecipientId)
SELECT TOP (500000) SendRecipientId, SendId, RecipientId
FROM #SendRecipients AS s
WHERE NOT EXISTS (
SELECT 1 FROM MyTargetTable AS t
WHERE t.Id = s.SendRecipientId
)
ORDER BY SendRecipientId
SELECT @rc = @@ROWCOUNT, @Total += @@ROWCOUNT;
COMMIT TRANSACTION;
END;