I have xml in following format
<Entity> <name>John</name> <aliases><alias>Johnny</alias></aliases> <aliases><alias>Johnson</alias></aliases> </Entity> <Entity> <name>Smith</name> <aliases><alias>Smithy</alias></aliases> <aliases><alias>Schmit</alias></aliases> </Entity>
I want to insert them in table so the table should have 4 records in the example.
columns are name and alias.
name | alias John | Johnny John | Johnson Smith| Smithy Smith| Schmit
How can I achive this using cursor or something else?
What i have tried. In cursor for entity i try insert alias value,but only first alias is taken.
insert into TESTTABLE (EntityID,Alias) select @EntityID as EntityID, Alias from OpenXml(@ixml, '/Aliases',2) with ( Alias varchar(255) '.' )
Advertisement
Answer
DECLARE @XML AS XML= N' <Entity> <name>John</name> <aliases><alias>Johnny</alias></aliases> <aliases><alias>Johnson</alias></aliases> </Entity> <Entity> <name>Smith</name> <aliases><alias>Smithy</alias></aliases> <aliases><alias>Schmit</alias></aliases> </Entity>' INSERT INTO @tblTest(firstName,LastName) SELECT t1.c.value('../name[1]','varchar(100)') As FirstName,t1.c.value('alias[1]','varchar(50)') as SecondName FROM @xml.nodes('/Entity/aliases') t1(c)