gustavlasko
asked on
Comingling relational and XML data
I have a table that consists of a few keys and an XML fragment. I need to decompose that xml fragment and load it into a new table, with one row for each XML node. The XML node needs to be broken into 2 columns. That's the easy part. For each row of the XML data in the new table, I also need to populate the column with the keys from the original table. I have been unable to find a way to do this without a very slow cursor going through each row in the source table and inserting each node along with its keys into the destination table.
Example of a row in the source table:
IsntanceID PatientID EncounterID XMLData
12867 46 63481 (XML Fragment stored in this column shown below)
XML Fragment:
<finding>
<item id="314" sectionname="HistoryofPres entIllness Section" />
</finding>
<finding>
<item id="353" sectionname="HistoryofPres entIllness Section" />
</finding>
<finding>
<item id="341" sectionname="HistoryofPres entIllness Section" />
</finding>
<finding>
<item id="650" sectionname="HistoryofPres entIllness Section" />
</finding>
<finding>
<item id="1613" sectionname="ChiefComplain tSection" />
</finding>
<finding>
<item id="663" sectionname="ChiefComplain tSection" />
</finding>
So once transformed, this would end up with 6 rows in the destination table, looking like the following:
IsntanceID PatientID EncounterID id sectionname
12867 46 63481 314 HistoryofPresentIllnessSec tion
12867 46 63481 353 HistoryofPresentIllnessSec tion
12867 46 63481 341 HistoryofPresentIllnessSec tion
12867 46 63481 650 HistoryofPresentIllnessSec tion
12867 46 63481 1613 ChiefComplaintSection
12867 46 63481 663 ChiefComplaintSection
Example of a row in the source table:
IsntanceID PatientID EncounterID XMLData
12867 46 63481 (XML Fragment stored in this column shown below)
XML Fragment:
<finding>
<item id="314" sectionname="HistoryofPres
</finding>
<finding>
<item id="353" sectionname="HistoryofPres
</finding>
<finding>
<item id="341" sectionname="HistoryofPres
</finding>
<finding>
<item id="650" sectionname="HistoryofPres
</finding>
<finding>
<item id="1613" sectionname="ChiefComplain
</finding>
<finding>
<item id="663" sectionname="ChiefComplain
</finding>
So once transformed, this would end up with 6 rows in the destination table, looking like the following:
IsntanceID PatientID EncounterID id sectionname
12867 46 63481 314 HistoryofPresentIllnessSec
12867 46 63481 353 HistoryofPresentIllnessSec
12867 46 63481 341 HistoryofPresentIllnessSec
12867 46 63481 650 HistoryofPresentIllnessSec
12867 46 63481 1613 ChiefComplaintSection
12867 46 63481 663 ChiefComplaintSection
ASKER
I gave that a shot (edited id to be tc.c.value('@sectionname', 'varchar(5 0)') as id is in the xml):
select t.intstanceid,
patientid , encounterid ,
tc.c.value('@id','varchar( 50)') as ID,
tc.c.value('@sectionname', 'varchar(5 0)') as SectionName
from SourceTable t
cross apply t.XMLData.nodes('/finding/ item') tc(c)
This doesn't yield any results, though. I'm going to continue to look into using apply, as this seems like my best bet.
select t.intstanceid,
patientid , encounterid ,
tc.c.value('@id','varchar(
tc.c.value('@sectionname',
from SourceTable t
cross apply t.XMLData.nodes('/finding/
This doesn't yield any results, though. I'm going to continue to look into using apply, as this seems like my best bet.
It is...and it works for me. Here is the sample data I used:
create table #t(intstanceid int, patientid int, encounterid int, id int, sectionname xml)
insert into #t
select 12867,46,63481,314, '<finding>
<item id="314" sectionname="HistoryofPresentIllnessSection" />
</finding>
<finding>
<item id="353" sectionname="HistoryofPresentIllnessSection" />
</finding>
<finding>
<item id="341" sectionname="HistoryofPresentIllnessSection" />
</finding>
<finding>
<item id="650" sectionname="HistoryofPresentIllnessSection" />
</finding>
<finding>
<item id="1613" sectionname="ChiefComplaintSection" />
</finding>
<finding>
<item id="663" sectionname="ChiefComplaintSection" />
</finding>'
select t.intstanceid,
patientid , encounterid , id ,
tc.c.value('@sectionname','varchar(50)')
from #t t
cross apply t.sectionname.nodes('/finding/item') tc(c)
ASKER
It looks like that solution requires an ID column that will match up with the ID attribute in the XML... my source table doesn't have that available. I need to get both the id and sectionname from the XML
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you. This returns the results I am looking for.
select t.intstanceid,
patientid , encounterid , id ,
tc.c.value('@sectionname',
from SourceTable t
cross apply t.XMLData.nodes('/finding/