What you are describing looks like a hierarchy to me. It's way too complex a subject to get into here. Get yourself a copy of "SQL for Smarties" by Joe Celko. He devotes a large chapter to the subject, and I understand he's about to publish an entire book on trees and hierarchies in SQL.
You might want to do some research into "nested sets" and/or "adjacency list". A Google search on these terms will point you in the right direction.
Note that these solutions would require a redesign of your tables. If you can't do that, you may find that even though you think the temporary table/loop approach is the same thing as a cursor, it really isn't - the cursor consumes
way more resources than the temp table and the enclosing loop...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com