MSSQL Bulk insert nested xml with foreign key as identity column of first table



Ok so I have an xml as follows:



<Records>
<Record>
<Name>Best of Pop</Name>
<Studio>ABC studio</Studio>
<Artists>
<Artist>
<ArtistName>John</ArtistName>
<Age>36</Age>
</Artist>
<Artist>
<ArtistName>Jessica</ArtistName>
<Age>20</Age>
</Artist>
</Artists>
</Record>
<Record>
<Name>Nursery rhymes</Name>
<Studio>XYZ studio</Studio>
<Artists>
<Artist>
<ArtistName>Judy</ArtistName>
<Age>10</Age>
</Artist>
<Artist>
<ArtistName>Rachel</ArtistName>
<Age>15</Age>
</Artist>
</Artists>
</Record>
</Records>


This file may contain millions of records. My MS SQL database has the following 2 tables to store these records:



1. Record(RecordId [PK, identity, auto-increment],
Name,
Studio)

2. Artist(RecordId [Foreign Key refers Record.RecordId],
ArtistName,
Age)


Is it possible to bulk insert records in Record table, get the RecordIds and then bulk insert the Artist information in the Artist table in a single traversal of the xml using the xml nodes approach? I have been searching for an efficient way to do this for a long time but in vain. I have tried approaches similar to the ones described here and here, but i'm not able to get to the solution.


Any pointers in the direction of the solution will be of great help.


No comments:

Post a Comment