Prova qualcosa del genere, devi ancora usare la tabella temporanea ma non è male da leggere e fa il suo lavoro.
CREATE TABLE #tmp ( tmpID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, xmlData VARCHAR(255), EntityId INT ) DECLARE @t2 TABLE ( tmpID INT, EntityId INT ) MERGE dbo.EntityMaster AS EM USING ( SELECT tmpID, xmlData, EntityId FROM #tmp ) AS X ON EM.EntityId = X.EntityId WHEN NOT MATCHED THEN INSERT (EntityType) VALUES (X.xmlData) OUTPUT X.tmpID, INSERTED.EntityId INTO @t2 (tmpID, EntityId); UPDATE T SET EntityId = T2.EntityId FROM @t2 T2 INNER JOIN #tmp T ON T2.tmpID = T.tmpID