Quantcast
Channel: SQL Server Database Engine forum
Viewing all articles
Browse latest Browse all 12554

Need help to inserting 100 records in loop, continue where it stop from and break once the records are completed.

$
0
0

This is what I have so far:

AlterPROCEDURE ETL.ETLPurge @PurgeYearINT

AS

BEGIN

 DECLARE @BatchId INT=(SELECT BatchIdFROM Tracker)

 declare @Count int

 declare @batchsize int 

 set @batchsize = 100 

 

  --set @Count = @@rowcount

 

 SELECTDISTINCTIDENTITY(INT,1,1)AS ID, MC.ID

 INTO #tmp

 FROM Contact MC

 JOIN Extract CE

   ON MC.ExtractID= CE.ExtractID

 LEFTJOIN Application A

   ON MC.ID = A.ID

WHERE CE.Year< @PurgeYear  

 AND A.ApplicationIdISNULL 

--declare @counter bigint

--set @counter = 1

--while @counter < 500

--Begin

--while 1 = 1

--begin

Create NONCLUSTEREDINDEX nix_ID

on #tmp(ID)

--while 1=1

--begin

INSERT 

--Top (@batchsize)

INTO Table1(Values ………)

(

SELECTtop (@batchsize) 

@BatchID, 

Values ……..)

 FROM Contact MC

 innerjoin

      #tmp TK on MC.ContactID= TK.ContactID

            

   --where TK.ID between @batchsize and @ctr + 1

    )

 if@@ROWCOUNT< @batchsize

 break

 end

 -- --continue 

 -- --if @@ROWCOUNT = 0

 -- Break

end

 --end

--number of rows inserted should equal number of rows deleted.

 


Viewing all articles
Browse latest Browse all 12554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>