I am trying to speed up a query. I am selecting the top 1 record each run and then deleting it so I can select top again. this is too slow deleting What I think will speed it up is to just select the record number and incriminate the number each time eliminating the delete part.I thought I could access the recordnumber like this but it wont workselect * from #tempourfilenum where recordnumber =1....select top 1 @theourfile=rtrim(ltrim(ourfile)), @ourdate=rtrim(ltrim(editdate)) from #tempourfilenumWhile ( @theourfile is not null)begin select top 1 @theourfile=rtrim(ltrim(ourfile)),@ourdate=rtrim(ltrim(editdate)) from #tempourfilenum -- drop table ##temp2 drop table #temp2 --we now put the the ourfile and editdate in a table to use to get the right data --for each ourfile select into #temp2 from alladdressqlaw where ltrim(rtrim(ourfile))=ltrim(rtrim(@theourfile)) ORDER BY editdate DESC -- get counts to make sure there are records to write to the addresses select @mycount=count(ourfile) from #temp2 if @mycount >=1 begin --address2 --get the ourfile and date select top 1 @ourfile = rtrim(ltrim(ourfile)), @ourdate = ltrim(rtrim(editdate)) from #temp2 update a table delete from #temp2 where ltrim(rtrim(ourfile))=ltrim(rtrim(@theourfile)) and ltrim(rtrim(editdate))=ltrim(rtrim(@ourdate)) delete from #tempourfilenum where ltrim(rtrim(ourfile)) = ltrim(rtrim(@theourfile))select top 1 @theourfile=rtrim(ltrim(ourfile)) from #tempourfilenum