我有以下查询,返回4亿行。我想运行查询,以便循环并一次插入100万条记录。我可以得到循环查询。
insert into AST (DataAreaId, Name)
select f.DataAreaId, its.Name....... etc
from Transform.InventFin f
inner join Staging.INVENTSETTLEMENT its
on f.ITSRECID=its.RECID
and f.DataAreaId=its.DATAAREAID
答案 0 :(得分:1)
尝试以下操作(假设DataAreaId
是唯一的,如果不是,则需要在NOT EXISTS
中包含这些列。)
declare @Count int
set @Count = 1
while @Count > 0
begin
insert into AST (DataAreaId, Name)
select TOP (1000000) f.DataAreaId, its.Name....... etc
from Transform.InventFin f
inner join Staging.INVENTSETTLEMENT its
on f.ITSRECID=its.RECID
and f.DataAreaId=its.DATAAREAID
WHERE NOT EXISTS
(
SELECT 1 FROM AST A WHERE AST.DataAreaId = F.DataAreaId
)
set @Count = @@ROWCOUNT
end