Monday, March 6, 2017

Looping in SQL Server

DECLARE @cnt INT = 1;
WHILE @cnt < (select max(id) FROM [Item])
BEGIN  
 
  update [Stock]
  set [PurchaseQty] = (select [SaleQty] + [BalanceQty] FROM [Stock] where ItemId = @cnt)
  where ItemId = @cnt  
 
  SET @cnt = @cnt + 1;

END;