How to parse complex XML to multiple inserts and updates in SQL server? -


i have found examples of parsing xml inserts. these examples simple. this:

<person>     <name>martin</name> </person> <person>     <name>john</name> </person> 

but have xml similar - need have inserts other tables child elements.

<root>     <family>         <name>smith</name>         <address>some road 1</address>         <persons>             <person>                 <name>tina</name>                 <hobbies>                     <hobby>some hobby 1</hobby>                     <hobby>some hobby 2</hobby>                 </hobbies>             </person>             <person>                 <name>martin</name>                 <hobbies>                     <hobby>some hobby 1</hobby>                     <hobby>some hobby 2</hobby>                 </hobbies>             </person>         </persons>     </family>     <family>         <name>lane</name>         <address>some road 1</address>         <persons>             <person>                 <name>kevin</name>                 <hobbies>                     <hobby>some hobby 1</hobby>                     <hobby>some hobby 2</hobby>                 </hobbies>             </person>             <person>                 <name>julia</name>                 <hobbies>                     <hobby>some hobby 1</hobby>                     <hobby>some hobby 2</hobby>                 </hobbies>             </person>         </persons>     </family> </root> 

i need iterate through xml , first insert row table "families" after return id family , use foreign key in next insert person in table "persons" , same hobbies. think idea. , after "family" need update statements before moving on next family.

could point me in right direction ? appreciated.

unfortunately, sql server doesn't support multi-table insert, need single inserts this:

insert family       select f.node.value('name[1]', 'varchar(32)') name     @xml.nodes('/root/family') f(node)  insert person     select family.id familyid, p.node.value('name[1]', 'varchar(32)') name     @xml.nodes('/root/family') f(node)     cross apply f.node.nodes('persons/person') p(node)     inner join family on f.node.value('name[1]', 'varchar(32)') = family.name 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -