hi. here is the execution plan. some explanations:
@InsertedCumulatedDataIds:
A table filled at the beginning of the procedures. For this Ids we have to write records in table #tmpProductHistory.
The tables DetailledDataANP and Product are joined to the @InsertedCumulatedDataIds to get all the information necessary for the later insert.
In the subselect the content of table DetailledDataANP and CumulatedData are stored as XML for each of the records.
The #tmpProductHistory is copied to the permanent table later.
This is the query:
insert into #tmpProductHistory( ProductId, IdentCode, PartitionKey, ProductHistoryTypeId, ProductHistoryReasonId, EventContent )
select
anp.ProductId,
anpp.Identcode,
anp.PartitionKey,
1,
1,
(
select *
from billing.DetailledDataANP as Innen join
billing.CumulatedData as f on
f.CumulatedDataId = Innen.CumulatedDataId
Where Innen.DetailledDataANPId = anp.DetailledDataANPId
for XML Auto
) As EventContent
from billing.CumulatedData fact inner join
billing.DetailledDataANP anp on
fact.CumulatedDataId = anp.CumulatedDataId inner join
dbo.Product anpp on
anp.ProductId = anpp.ProductId
where fact.CumulatedDataId in ( Select ids.Id From @InsertedCumulatedDataIds as ids );