I've got a table like this:i int identitydesc varchar(20)score int
And I've got another table like this:dt datetime,top1 int,top2 int, top3 int
I'm looking to periodically get the top 3 "i" values from the first table, sorted descending by score, into the second table. The only way I can think of doing it is:declare table @t (i int,score int)declare @iVal1 int,@iVal2 int,@iVal3 intinsert into @t (i,score)select top 3 i,score from t1 order by score descselect top 1 @iVal1=i from @t order by score descdelete from @t where i=@iVal1select top 1 @iVal2=i from @t order by score descdelete from @t where i=@iVal2select top 1 @iVal3=i from @t order by score descinsert into @t2 (dt,top1,top2,top3) VALUES (getdate(),@iVal1,@iVal2,@iVal3)
That works (or least something just like it works; I just typed that in so forgive typos or stupid mistakes). However, it's pretty ugly and relatively slow.Is there any way to do this in one fell swoop, without the table variable and deletes and stuff?Thanks-b