It looks to me that you are recreating the temp table with each loop iteration. For now dump the SELECT... INTO..., create the temp table before the loop and do an INSERT INTO in the loop.
Down the road you ought to learn why procedural and loop based logic is not good in SQL and you should learn about set based processing.
If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry. Earnest Hemingway, A Farewell To Arms.