Do the temporary table operations within a transaction to ensure proper cleanup