由于有完整的交易日志,我正在像这样划分大的INSERT
:
DECLARE @rc INT = 1;
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
SET @rc = @@ROWCOUNT;
COMMIT TRANSACTION;
END;
如何获取插入的行总数?
答案 0 :(得分:3)
添加另一个变量来求和@@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;
答案 1 :(得分:0)
您可以尝试-更新查询。
DECLARE @rc INT = 0;
WHILE (1=1)
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
SET @rc = @rc + @@ROWCOUNT;
COMMIT TRANSACTION;
IF (@@ROWCOUNT = 0)
BEGIN
BREAK;
END
END;