If you have a large number of rows, it would be more efficient to use a set-based query such as this:;with cte as( select id,amount,amount as full_amount from rec where id=1 union all select r.id,r.amount,r.amount+c.full_amount from rec r inner join cte c on r.id = c.id+1)--select * from cte option (maxrecursion 0);update r set r.full_amount = c.amountfrom rec r inner join cte c on c.id = r.id option (maxrecursion 0);