我在Execute sql任务可执行文件的SSIS序列容器中有一个TSQL命令。 我需要将数据插入与PROD相同并附加在SQL 2016标准edt的同一实例上的存档数据库/表中。 我插入了约5671294条记录,同时更新了其他帮助/阶段表中的标记,以用于进一步的检查。 更新最后的71294条记录后,我的循环永无休止。在“活动”监视器上,我可以看到卡在UPDATE命令中,没有更新列。 另一个问题是,它的运行速度非常慢(简单恢复中的数据库)。
SET IDENTITY_INSERT ArchiveDB..Table ON
GO
DECLARE @InsertRowCnt INT
SET @InsertRowCnt = 1
DECLARE @InsertBatchSize INT
SET @InsertBatchSize=100000
WHILE (@InsertRowCnt > 0)
BEGIN
DECLARE @OutputTbl AS TABLE(BSGID INT)
INSERT INTO ArchiveDB..Table ([BSGID], [Identifier],[SystemCombinationItems], [TotalItems], [TotalBankers],[SpecialCombinationID], [ModifiedOn], [ModifiedBy], [CreatedOn])
OUTPUT inserted.BSGID INTO @OutputTbl(BSGID)
SELECT TOP (@InsertBatchSize) s.BSGID, s.Identifier,
s.SystemCombinationItems, s.TotalItems, s.TotalBankers, s.SpecialCombinationID, s.ModifiedOn,
s.ModifiedBy, s.CreatedOn
FROM ProdDB.dbo.Table AS s WITH (NOLOCK) INNER JOIN
ArchiveDB.dbo.StageTable AS t WITH (NOLOCK) ON s.BSGID = t.BSGID
WHERE t.IsInserted IS NULL
----Updating stage table contains 2 columns BSGID int,IsInserted int null
UPDATE arh
SET arh.IsInserted=1
FROM ArchiveDB.dbo.StageTable AS arh WITH(NOLOCK)
INNER JOIN @OutputTbl AS a
ON arh.BSGID=a.BSGID
WHERE arh.IsInserted is null
SET @InsertRowCnt = @@ROWCOUNT;
END
SET IDENTITY_INSERT ArchiveDB..Table OFF
GO
答案 0 :(得分:0)
SET IDENTITY_INSERT ArchiveDB..Table ON
GO
DECLARE @InsertRowCnt INT
--------------------------------
SELECT @InsertRowCnt=COUNT(*)
FROM ProdDB.dbo.Table AS s WITH (NOLOCK) INNER JOIN
ArchiveDB.dbo.StageTable AS t WITH (NOLOCK) ON s.BSGID = t.BSGID
WHERE t.IsInserted IS NULL
----------------------------
DECLARE @InsertBatchSize INT
SET @InsertBatchSize=100000
WHILE (@InsertRowCnt > 0)
BEGIN
DECLARE @OutputTbl AS TABLE(BSGID INT)
INSERT INTO ArchiveDB..Table ([BSGID], [Identifier],[SystemCombinationItems],
[TotalItems], [TotalBankers],[SpecialCombinationID], [ModifiedOn], [ModifiedBy],
[CreatedOn])
OUTPUT inserted.BSGID INTO @OutputTbl(BSGID)
SELECT TOP (@InsertBatchSize) s.BSGID, s.Identifier,
s.SystemCombinationItems, s.TotalItems, s.TotalBankers, s.SpecialCombinationID,
s.ModifiedOn,
s.ModifiedBy, s.CreatedOn
FROM ProdDB.dbo.Table AS s WITH (NOLOCK) INNER JOIN
ArchiveDB.dbo.StageTable AS t WITH (NOLOCK) ON s.BSGID = t.BSGID
WHERE t.IsInserted IS NULL
----Updating stage table contains 2 columns BSGID int,IsInserted int null
UPDATE arh
SET arh.IsInserted=1
FROM ArchiveDB.dbo.StageTable AS arh WITH(NOLOCK)
INNER JOIN @OutputTbl AS a
ON arh.BSGID=a.BSGID
WHERE arh.IsInserted is null
--------------------------------
SELECT @InsertRowCnt=COUNT(*)
FROM ProdDB.dbo.Table AS s WITH (NOLOCK) INNER JOIN
ArchiveDB.dbo.StageTable AS t WITH (NOLOCK) ON s.BSGID = t.BSGID
WHERE t.IsInserted IS NULL
----------------------------
END
SET IDENTITY_INSERT ArchiveDB..Table OFF
GO