Connection is a problem. Do not do this. Just scroll through the current table using some reasonable interval using the current clustered index. Something like:
Declare @idrange int; Set @idrange = 1; WHILE @idrange < 10000000 INSERT INTO Destination SELECT * FROM Source WHERE DocumentID between @idrange and @idrange + 999 ORDER BY Source.DocumentID Set @idrange = @idrange + 1000 End
Note that for best speed, remove all indexes (including the clustered index) from the destination table, then add indexes after all rows have been inserted.
EDIT : changed the span of the range to prevent overlapping (since BETWEEN includes endpoints)
Final clarification: The common point of my example script is that you just want to go through your current records in some reasonable order and put them in a new table in packages. There is no reason to keep checking the destination table every time, as you should already know what you put there and what remains. In most cases, it makes sense to use a clustered index (if one exists), since this means that it can move through the physical order of the table without searching through bookmarks. If there is no cluster in the table, just use whatever makes the most sense (your PC, maybe).
Bradc
source share