Solved

Comingling relational and XML data

Posted on 2008-06-16
6
189 Views
Last Modified: 2010-04-21
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="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>


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              HistoryofPresentIllnessSection
12867                   46                    63481                 353              HistoryofPresentIllnessSection
12867                   46                    63481                 341             HistoryofPresentIllnessSection
12867                   46                    63481                 650             HistoryofPresentIllnessSection
12867                   46                    63481                 1613           ChiefComplaintSection
12867                   46                    63481                 663             ChiefComplaintSection
0
Comment
Question by:gustavlasko
  • 3
  • 3
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21797796
this should do it:

select t.intstanceid,
patientid , encounterid , id ,
tc.c.value('@sectionname','varchar(50)')
from SourceTable t
cross apply t.XMLData.nodes('/finding/item') tc(c)
0
 

Author Comment

by:gustavlasko
ID: 21798544
I gave that a shot (edited id to be tc.c.value('@sectionname','varchar(50)') as id is in the xml):

select t.intstanceid,
patientid , encounterid ,
tc.c.value('@id','varchar(50)') as ID,
tc.c.value('@sectionname','varchar(50)') 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.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21798928
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)

Open in new window

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:gustavlasko
ID: 21799038
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
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21799281
whoops...try this instead:

drop table #t
create table #t(intstanceid int, patientid int, encounterid int, sectionname xml)

insert into #t
select 12867,46,63481, '<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 ,  
tc.c.value('@sectionname','varchar(50)'),
tc.c.value('@id','INT')
from #t t
cross apply t.sectionname.nodes('/finding/item') tc(c)
0
 

Author Closing Comment

by:gustavlasko
ID: 31467782
Thank you. This returns the results I am looking for.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now