The ERP system we're migrating to requires csv files with 5,000 or less rows for the GL. The debit and credit transactions within each file must balance to zero. There are multiple debit and credit transaction rows that share a common transaction ID.
Using offset and fetch next I've been able to extract 5000 rows at a time, however the credits and debits do not balance.
Data Example:
TranID Credit Debit Balance Account#
1 250 0 250 ABC
1 0 250 0 DEF
2 0 100 -100 GHI
2 50 0 -50 JKL
2 50 0 0 MNO
declare @batchsize INT = 5000,
@loopcount INT = 0;
while (@loopcount*@batchsize < (select count(*) from [Apps2].[dbo].[GLTrans]))
begin
SELECT * FROM [Apps2].[dbo].[GLTrans]
ORDER BY tranID
offset (@batchsize * @loopcount) rows
fetch next (@batchsize) rows only
set @loopcount= @loopcount + 1
end