我在表中有xml列,我想解析xml并将值插入新表
如何将xml值插入表中?
CREATE TABLE [dbo].[PubmedMeshDescriptors](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PMID] [int] NULL,
[Descriptor] [nvarchar](400) NULL,
[DescriptorIsMajor] [char](1) NULL,
[Qualifier] [nvarchar](400) NULL,
[QualifierIsMajor] [char](1) NULL
)
并且另一个表中的xml列看起来像这样
<MedlineCitation Owner="NLM" Status="MEDLINE">
<PMID Version="1">1</PMID>
<MeshHeadingList>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Aldehyde Oxidoreductases</DescriptorName>
<QualifierName MajorTopicYN="N">metabolism</QualifierName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Animals</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Carbon Dioxide</DescriptorName>
<QualifierName MajorTopicYN="N">blood</QualifierName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Formates</DescriptorName>
<QualifierName MajorTopicYN="N">blood</QualifierName>
<QualifierName MajorTopicYN="Y">poisoning</QualifierName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Haplorhini</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Hydrogen-Ion Concentration</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Methanol</DescriptorName>
<QualifierName MajorTopicYN="N">blood</QualifierName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Methods</DescriptorName>
</MeshHeading>
<MeshHeading>
<DescriptorName MajorTopicYN="N">Pseudomonas</DescriptorName>
<QualifierName MajorTopicYN="N">enzymology</QualifierName>
</MeshHeading>
</MeshHeadingList>
</MedlineCitation>
我怎么能用sql查询呢?
答案 0 :(得分:2)
将xml放入xml变量并尝试以下内容:
INSERT PubmedMeshDescriptors (PMID, Descriptor, DescriptorIsMajor, Qualifier, QualifierIsMajor)
SELECT
PMIDNode.value('.', 'int') as PMID,
DescriptorNode.value('.', 'nvarchar(400)') as Descriptor,
DescriptorNode.value('(./@MajorTopicYN)[1]', 'char(1)') as DescriptorIsMajor,
QualifierNode.value('.', 'nvarchar(400)') as Qualifier,
QualifierNode.value('(./@MajorTopicYN)[1]', 'char(1)') as QualifierIsMajor
FROM @xml.nodes('/MedlineCitation') AS citation(citationNode)
CROSS APPLY
citationNode.nodes('./PMID')
AS PMID(PMIDNode)
CROSS APPLY
citationNode.nodes('./MeshHeadingList/MeshHeading')
AS MeshHeading(MeshHeadingNode)
OUTER APPLY
MeshHeadingNode.nodes('./DescriptorName')
AS Descriptor(DescriptorNode)
OUTER APPLY
MeshHeadingNode.nodes('./QualifierName')
AS Qualifier(QualifierNode)