I have two tables
tmpEntityAddress
EntityId Address
________ _______
5 <Address />
5 <Address />
7 <Address />
tmpEntityAddresses
EntityId XML
________ _______
5 <Addresses />
5 <Addresses />
I want to group and insert the various addresses documents into the single addresses document in the second table. With a rough and basic schema looking like:
<Addresses>
<Address>
<Street />
<PostCode />
</Address>
<Address>
<Street />
<PostCode />
</Address>
</Addresses>
I can't quite wrap my head around how I would use the XML DML language features to do this in SQL such as XML insert etc https://learn.microsoft.com/en-us/sql/t-sql/xml/xml-data-modification-language-xml-dml
I was thinking that I could do the following:
update tmpEntityAddresses
set
XML.modify('insert sql:column("Address") into (/Addresses)[1]'
from
tmpEntityAddresses
join #tmpEntityAddresses on tmpEntityAddresses.EntityId = tmpEntityAddress.EntityId
But it seems to only add a single row from #tmpEntityAddress which isn't what I want as I need the whole collection.
Is this possible with SQL? If so how can this be achieved?