You can handle any number of nodes that might repeat - but mind you, this will always create numerous rows for a single entry <Physician>
.
Try this:
DECLARE @Content TABLE (ID INT NOT NULL, XmlDAta XML)
INSERT INTO @content VALUES(1, '<root>
<Physicians>
<name>Dr. Excellent</name>
<picture></picture>
<gender>Male</gender>
<langAccept>English</langAccept>
<langAccept>Spanish</langAccept>
<insAccept>Aetna</insAccept>
<insAccept>BCBS</insAccept>
<specialty></specialty>
<specialty2></specialty2>
<specialty3></specialty3>
</Physicians>
</root>')
SELECT
ID,
PhysicianName = XC.value('(name)[1]', 'varchar(50)'),
Gender = XC.value('(gender)[1]', 'varchar(50)'),
LangSpoken = XLang.value('.', 'varchar(20)'),
InsAccepted = XIns.value('.', 'varchar(50)')
FROM
@Content
CROSS APPLY
XmlData.nodes('/root/Physicians') AS XT(XC)
CROSS APPLY
XC.nodes('langAccept') AS XT2(XLang)
CROSS APPLY
XC.nodes('insAccept') AS XT3(XIns)
By using the .nodes()
on both the langAccept
and insAccept
inside the <Physician>
node, you get all the defined values - but you end up with several relational rows for a single <Physican>
node:
Update: to get the data from your own existing table, use this:
SELECT
ID,
PhysicianName = XC.value('(name)[1]', 'varchar(50)'),
Gender = XC.value('(gender)[1]', 'varchar(50)'),
LangSpoken = XLang.value('.', 'varchar(20)'),
InsAccepted = XIns.value('.', 'varchar(50)')
FROM
[MyDB].[dbo].Content
CROSS APPLY
CAST(content_html AS XML).nodes('/root/Physicians') AS XT(XC)
CROSS APPLY
XC.nodes('langAccept') AS XT2(XLang)
CROSS APPLY
XC.nodes('insAccept') AS XT3(XIns)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…