I know this isn't the question that was asked, but...
Is there a reason this couldn't be run from the two master tables, skipping the #Temp table?
Edit: This question was really for the OP, not all the magnanimous responders that were helping him out. 😉