Avoiding a cursor in mssql, with a loop

set @LoopIndexer = 1
set @LoopId = 1
 
-- loop through each item
while (@LoopIndexer<=@CountTtbwsid)
begin
 
-- find the next available row, avoiding a cursor
while not exists (select @LoopId from #ttbwsid where id = @LoopId)
set @LoopId = @LoopId + 1
 
-- work to be done inside cursor
-- value for row can be retrieved with a simple select
 
set @LoopIndexer = @LoopIndexer + 1
set @LoopId = @LoopId + 1
end

Comments