How to expand following XML into a table in TSQL?

时间:2016-12-09 12:52:02

标签: xml tsql sql-server-2012

I am not very familiar with XML in SQL server. I need to expand the following XML to a table, but I don't know how to do that. Can someone help me?

<MainNode>
    <Type>Type1</Type>
    <Group>Group1</Group>
    <IDFile>File1</IDFile>
    <IDFile>File2</IDFile>
    <IDFile>File3</IDFile>
    <IDFile>File4</IDFile>
    <XP>XP1</XP>
    <SubNode>
        <Data1>Data1</Data1>
        <Data2>Data2</Data2>
    </SubNode>
    <Parameters>
        <Parameter>
            <Name>PName1</Name>
            <Value>PVal1</Value>
        </Parameter>
        <Parameter>
            <Name>PName2</Name>
            <Value>PVal2</Value>
          </Parameter>
    </Parameters>         
</MainNode>

My main issue is how to expand sub nodes which contain more than one element. I also have repeating nodes with unknown number.

I use .nodes() and .value() functions

My code looks like:

declare @XML xml = '<MainNode>
    <Type>Type1</Type>
    <Group>Group1</Group>
    <IDFile>File1</IDFile>
    <IDFile>File2</IDFile>
    <IDFile>File3</IDFile>
    <IDFile>File4</IDFile>
    <XP>XP1</XP>
    <SubNode>
        <Data1>Data1</Data1>
        <Data2>Data2</Data2>
    </SubNode>
    <Parameters>
        <Parameter>
            <Name>PName1</Name>
            <Value>PVal1</Value>
        </Parameter>
        <Parameter>
            <Name>PName2</Name>
            <Value>PVal2</Value>
          </Parameter>
    </Parameters>         
</MainNode>';

SELECT 
    Child.value('(Type)[1]', 'VARCHAR(100)') AS [Type], 
    Child.value('(Group)[1]', 'VARCHAR(10)') AS [Group] 
FROM
    @XML.nodes('/MainNode') AS N(Child);

I need something like:

Type1 | Group1 | XP1 | Param1.1 Name | Param1.1 Value
Type1 | Group1 | XP1 | Param1.2 Name | Param1.2 Value
Type2 | Group2 | XP2 | Param2.1 Name | Param2.1 Value

...

1 个答案:

答案 0 :(得分:2)

您的问题不是很清楚......您的XML中有许多不同的数据结构......在一个查询中检索所有数据结构没有多大意义。

在下面的代码中,我向您展示了如何从中获取一些东西。我希望你能获得足够的模板,自己管理其余模板。

declare @XML xml = 
'<MainNode>
  <Type>Type1</Type>
  <Group>Group1</Group>
  <IDFile>File1</IDFile>
  <IDFile>File2</IDFile>
  <IDFile>File3</IDFile>
  <IDFile>File4</IDFile>
  <XP>XP1</XP>
  <SubNode>
    <Data1>Data1</Data1>
    <Data2>Data2</Data2>
  </SubNode>
  <Parameters>
    <Parameter>
      <Name>PName1</Name>
      <Value>PVal1</Value>
    </Parameter>
    <Parameter>
      <Name>PName2</Name>
      <Value>PVal2</Value>
    </Parameter>
  </Parameters>
</MainNode>';

- XQuery谓词将读取<MainNode>下方名称为“IDFile”的所有节点:

SELECT 
    idf.value('.', 'NVARCHAR(10)') AS IDFile
FROM
    @XML.nodes('/MainNode/*[local-name()="IDFile"]') AS N(idf);

- 键值对:在<MainNode><SubNode>内有KVP:

SELECT 
    SubNode.value('local-name(.)', 'NVARCHAR(100)') AS NodeName, 
    SubNode.value('.', 'NVARCHAR(10)') AS NodeValue
FROM
    @XML.nodes('/MainNode/SubNode/*') AS N(SubNode);

- 在<MainNode/Parameters>内,有一个典型的1:n相关子结构:

SELECT 
    Prm.value('Name[1]', 'NVARCHAR(100)') AS ParamterName, 
    Prm.value('Value[1]', 'NVARCHAR(10)') AS ParameterValue
FROM
    @XML.nodes('/MainNode/Parameters/Parameter') AS N(Prm);