--Please Try This, replace your table and column name if not matching
DECLARE @Counter as int=1
Declare @Stock as int=100
declare @StockRecieved as table(Sr int, BillNo int)
insert into @StockRecieved
SELECT ROW_NUMBER() over(order by [Item_Name], [Bill No]) Sr,[Bill No]
FROM [dbo].[stock_recieve]
WHILE(@Stock>0)
BEGIN
declare @DiffInOut int
declare @StockToBeUpdated int
declare @BILLNo int
select @BILLNo= BillNo from @StockRecieved where Sr=@Counter
select @DiffInOut=[Stock_in]-[Stock_out] FROM [dbo].[stock_recieve] where [Bill No]=@BILLNo
if (@Stock>=@DiffInOut)
BEGIN
SET @StockToBeUpdated=@DiffInOut
SET @Stock=@Stock-@DiffInOut
END
ELSE
BEGIN
SET @StockToBeUpdated=@Stock
SET @Stock=0
END
UPDATE [stock_recieve] set [Stock_out]=[Stock_out]+@StockToBeUpdated where [Bill No]=@BILLNo
SET @Counter=@Counter+1;
END
--To confirm
SELECT * FROM [dbo].[stock_recieve] order by [Item_Name], [Bill No]