I want to avoid looping from the below query.Now it takes long to retrieve the results from a table with 100000 records.
@iMAX int,
@rowId int,
@currCode varchar(20),
@lastCode varchar(20),
@amount money,
@total money
SET @rowId=1
SET @iMAX=(Select count(RowId) from Table1)
WHILE(@rowId<=@iMAX)
-- WHILE iteration
BEGIN
SELECT @Code=Code,@amount=Amount FROM Table1 WHERE RowId=@rowId
if @lastCode is null or @lastCode <> @currCode
begin
set @total = 0
set @lastCode = @currCode
end
set @total = @total + @amount
update Table1 set Balance = OpBalance + @total where RowId = @rowId
SET @rowId=@rowId+1
END