下面是XML数据。
<GcsDocument xmlns="http://www.globalcaresolutions.com/Report/GcsDocument">
<Patient>
<PatientID>7a814612-028d-11e9-b2ed-14b31f267fc3</PatientID>
<PatientVisitID>e7713bf7-0369-11e9-be5e-5065f31c4cb0</PatientVisitID>
<VisiblePatientID>00563904</VisiblePatientID>
<Allergies>Seafood</Allergies>
<Person>
<DisplayNameL>Natuel, Anne Richie Paco</DisplayNameL>
<FirstNameL>Anne Richie</FirstNameL>
<LastNameL>Natuel</LastNameL>
<DisplayNameE>Natuel, Anne Richie Paco</DisplayNameE>
<FirstNameE>Anne Richie</FirstNameE>
<LastNameE>Natuel</LastNameE>
<Gender>Female</Gender>
<Race>Unknown</Race>
<Age>41</Age>
<AgeUnits>yrs</AgeUnits>
<DOB>1977-03-09T00:00:00</DOB>
<VisibleAge>41 y</VisibleAge>
</Person>
<VisiblePatientVisitID>216283</VisiblePatientVisitID>
<VisitDate>2018-12-19T16:41:41</VisitDate>
<VisitType>Inpatient Visit</VisitType>
<Location>9A Bed 902</Location>
</Patient>
</GcsDocument>
想提取以下数据:
PatientID PatientVisitID VisiblePatientID Allergies DisplayNameL Gender Age
以此类推。
答案 0 :(得分:0)
尝试这样的事情:
; WITH XMLNAMESPACES (DEFAULT 'http://www.globalcaresolutions.com/Report/GcsDocument')
SELECT
ID = XC.value('(PatientID)[1]', 'UNIQUEIDENTIFIER'),
VisitID = XC.value('(PatientVisitID)[1]', 'UNIQUEIDENTIFIER'),
VisiblePatientID = xc.value('(VisiblePatientID)[1]', 'INT'),
Allergies = XC.value('(Allergies)[1]', 'VARCHAR(100)'),
DisplayName = XC.value('(Person/DisplayNameL)[1]', 'VARCHAR(100)'),
Gender = XC.value('(Person/Gender)[1]', 'VARCHAR(100)'),
Age = XC.value('(Person/Age)[1]', 'INT')
FROM
YourTableNameGoesHere
CROSS APPLY
YourXmlColumm.nodes('/GcsDocument/Patient') AS XT(XC)
要注意的要点:
YourTableNameGoesHere
(您未在问题中提供该名称)YourXmlColumn
对.nodes()
的调用将创建XML片段的“伪内联表”-每个匹配的XML节点一个(例如,此处:根节点内的每个<Patient>
XML节点一个)。然后,您可以使用SQL Server中的XQuery支持“进入”该XML片段并提取各个片段。