Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Batched SQL insert

Author  Topic 

Bjcascone
Starting Member

37 Posts

Posted - 2011-02-10 : 15:44:48
I have 2 tables the first is the main table (+1.8 billion rows) the second is a loading table (+77 million rows) I need to insert the 2nd into the first. When doing an insert the log file blows up to over 500 GB and i run out of room in the DB. I think i need to do a batched insert that will insert X (1 million rows) then truncate the log file.

My questions are:

1) is this the best approach?

2) if it is the best approach what would the syntax be for this

*the table columns are identical.

Thanks in advance for your help!

-Brian

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-10 : 15:47:30
I do batches in 1,000-10,000 chunks. The smaller the chunk, the longer it'll take, but that's what I want in production to manage the tlog, rollback time, and impact to customers.

Use a WHILE loop with INSERT/SELECT TOP @batchSize. Use @@ROWCOUNT to determine when to stop.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -