Instead of using temp tables in this manner, I think what rjv_rnjn suggested would be much more efficient with same output result.