0

I have the below query which returns 400 million rows. I want to run the query so it loops through and inserts 1 million records at a time. Please can I get the loop query.

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 

1 Answer 1

1

Try like following(Assuming that DataAreaId is Unique, if not you need to include those columns in 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
Sign up to request clarification or add additional context in comments.

2 Comments

DataAreaID isn't the primary key so will this still apply?
You need to make it unique to avoid any duplicates, for this you will be required to add the condition to your to WHERE AST.DataAreaId = F.DataAreaId and AST.Col1= tbl.Col1 AND AST.Col2=tbl.Col2. like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.