i have one xml like,
Declare @xmldata xml;
set @xmldata ='
<Customers>
<Id>1</Id>
<Name>foo</Name>
<UserName>TestUser</UserName>
<Password>pwd</Password>
<Contacts>
<FirstName>david</FirstName>
<LastName>lawr</LastName>
</Contacts>
<Contacts>
<FirstName>john</FirstName>
<LastName>peter</LastName>
</Contacts>
</Customers>';
I want to insert this above xml doc to 2 sql tables. In First Table(Table1) have the fields like Id, Name, UserName, Password fields. and the Second Table(Table2) have the fields like CustomerId, FirstName, LastName.
This xml doc have more than one child nodes like Contacts. How to get the Node values and child node values from this xml, and want to insert this data to Table1 and Table2.
Note : If the Contact child node comes 2 time means, we will insert the 2 rows in Table2..
Try something like this:
Output from the
SELECTstatement is something like:Second query:
Output from this
SELECTis: