在SQL Server 2008 R2中查询复杂的XML

时间:2014-02-24 03:11:45

标签: sql sql-server xml sql-server-2008 tsql

我正在尝试解析SQL Server 2008 R2中的XML并且我遇到了一些问题。我试图解析每个父节点的项目,但我似乎无法准确的数据。 XML数据如下:

 <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Project.xsd" Name="Job">
  <Design>
    <Catalog>
      <Identification>
        <Name>Group1</Name>
      </Identification>
      <FeatureSet ID="IDFS-1" Type="Style">
        <Description>Style1</Description>
      </FeatureSet>
      <Item ID="IDI-1-2730">
        <UserCode>Item1</UserCode>
        <LineItemNumber>1</LineItemNumber>
      </Item>
      <Item ID="IDI-1-1595">
        <UserCode>Item2</UserCode>
        <LineItemNumber>2</LineItemNumber>
      </Item>
    </Catalog>
    <Catalog>
      <Identification>
          <Name>Group2</Name>
      </Identification>
      <FeatureSet ID="IDFS-1" Type="Style">
        <Description>Style2</Description>
      </FeatureSet>
      <Item ID="IDI-1-2730">
        <UserCode>Item3</UserCode>
        <LineItemNumber>1</LineItemNumber>
      </Item>
    </Catalog>
  </Design>
</Project>

这是我到目前为止的SQL,但似乎没有起作用。

select
    x.d.query('./UserCode').value('.','char(40)') as UserCode
    ,x.d.query('./LineItemNumber').value('.','char(40)') as lineitemnumber
    ,i.d.query('./Name').value('.','nvarchar(200)') as [Group]
    ,u.d.query('./Description').value('.','nvarchar(200)') as Style
from 
    @xml.nodes('/Project/Design/Catalog/Item') as x(d)
outer apply 
    @xml.nodes('/Project/Design/Catalog/Identification') as i(d)
outer apply 
    @xml.nodes('/Project/Design/Catalog/FeatureSet') as u(d)

显然,我正在使用XML数据类型将@xml设置为上面的xml数据。

以下是我得到的结果:

UserCode    lineitemnumber  Group   Style
Item1       1               Group1  Style1
Item1       1               Group1  Style2
Item1       1               Group2  Style1
Item1       1               Group2  Style2
Item2       2               Group1  Style1
Item2       2               Group1  Style2
Item2       2               Group2  Style1
Item2       2               Group2  Style2
Item3       1               Group1  Style1
Item3       1               Group1  Style2
Item3       1               Group2  Style1
Item3       1               Group2  Style2

我在寻找的是:

UserCode    lineitemnumber  Group   Style
Item1       1               Group1  Style1
Item2       2               Group1  Style1
Item3       1               Group2  Style2

每个目录只有一个标识/描述,并且也只有一个功能集/描述

2 个答案:

答案 0 :(得分:1)

select
     x.d.query('./UserCode').value('.','char(40)') as UserCode
    ,x.d.query('./LineItemNumber').value('.','char(40)') as lineitemnumber
    ,x.d.query('../Identification/Name').value('.','nvarchar(200)') as [Group]
    ,x.d.query('../FeatureSet/Description').value('.','nvarchar(200)') as Style
from @xml.nodes('/Project/Design/Catalog/Item') as x(d)

答案 1 :(得分:1)

如@Backs所述,父节点查询可能表现不佳,因此您可以通过从父级到子级的CROSS APPLYING来避免它,然后您可以根据需要引用任一部分:

select
    -- these 2 are from the child (Item)
    c.i.value('(UserCode/text())[1]', 'char(40)') as UserCode,
    c.i.value('(LineItemNumber/text())[1]', 'char(40)') as lineitemnumber,

    -- these are from the parent (Catalog)
    d.c.value('(Identification/Name/text())[1]', 'nvarchar(200)') as [Group],
    d.c.value('(FeatureSet/Description/text())[1]', 'nvarchar(200)') as Style
from
    @xml.nodes('/Project/Design/Catalog') d(c) -- the parent node
    cross apply d.c.nodes('Item') c(i) -- the Item nodes under the Catalog parent

相比之下,这有一个更好的估计计划