Skip to content

Instantly share code, notes, and snippets.

@RajatGoyal
Last active May 23, 2017 06:46
Show Gist options
  • Save RajatGoyal/555152b3ce5b052b539686fee22f9389 to your computer and use it in GitHub Desktop.
Save RajatGoyal/555152b3ce5b052b539686fee22f9389 to your computer and use it in GitHub Desktop.
copy data in batches from one table another, so that transfer does not happen in a single transactions
DECLARE @BatchSize INT = 20000
declare @StartId = 0;
declare @MaxId;
select @MaxId = max(Id) from [dbo].[SOURCE] .
WHILE 1 = 1
BEGIN
INSERT INTO [dbo].[Destination]
SELECT *
FROM [dbo].[SOURCE]
WHERE id between @StartId and @StartId + @BatchSize;
SET @StartId = @StartId + @BatchSize
IF @StartId > @MaxId BREAK
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment