CREATE TABLE #Tempcard
(
Clubcard BIGINT NULL,
DateTime DATETIME NULL
)
WHILE 1=1
BEGIN
WITH EventsTop1000
AS
(
SELECT top 200 Clubcard,DateTime
FROM Clubcard
WHERE(DATEDIFF(DAY ,Clubcard.DateTime ,getdate())>120))
DELETE EventsTop200
OUTPUT DELETED.*
INTO #Tempcard;
IF (@@ROWCOUNT = 0)
BREAK;
END
Here I am doing a Batch insertion. Once I insert the records to the #Tempcard table, I do not want to delete the data from the table Clubcard, but I need to fetch the next set of data from the Clubcard and again insert the data to #Tempcard.
If my records are 1020, I can see only 1000 records getting inserted, the rest 20 is not getting inserted.
Please let me know how to solve the issue.
If I understand correctly, you want to round number of rows to 200:
You might consider using an order by to keep things consistent.
If you are saying that you cannot insert all the records from Clubcard into #tempcard, I’ve tried it and it works correctly. #tempcard has 1020 records. I had to change CTE name though to EventsTop200.
UPDATE: question clarified.
If Clubcard can have nulls add real primary key to temporary table #Tempcard.
To copy table in batch using t-sql code:
There might be problems. #Tempcard might miss records inserted after procedure copied their place in a batch, or might have records deleted after copying.