declare @t as table (
id0 int identity(1,1),id int,qty int,cf int
)
declare @t2 as table(
id int,qty int,cf int
)
insert into @t select *from TEST6
declare @id int = (select (min(id0)) from @t)
declare @idmax int =(select (max(id0)) from @t)
declare @qtyTemp as int
declare @qtycfTemp as int
while(@id<=@idmax)
begin
select @qtyTemp = qty,@qtycfTemp =cf from @t where id0 = @id
while(@qtyTemp>0)
begin
if(@qtyTemp>@qtycfTemp)
begin
insert into @t2 select id,qty,@qtycfTemp from @t where id0 = @id
set @qtyTemp = @qtyTemp-@qtycfTemp
end
else
begin
insert into @t2 select id,qty,@qtyTemp from @t where id0 = @id
set @qtyTemp = 0
end
end
set @id=@id+1
end