如何将此xml字段转换为表?

时间:2011-01-10 19:13:23

标签: xml sql-server-2005

我在sql server 2005的XML字段中得到了这个

<Details>
  <Attribute Type="org">6800</Attribute>
  <Attribute Type="fac">100</Attribute>
  <Attribute Type="fac">200</Attribute>
</Details>

将此转换为表格的语法是什么,比如

 col_Type col_value
    org      6800
    fac      100
    fac      200

我在编写查询时遇到单身错误

1 个答案:

答案 0 :(得分:2)

找到了怎么做

如果有人想知道如何:

    declare @XmlContent xml

    set @XmlContent = '<Details>
      <Attribute Type="org">6800</Attribute>
      <Attribute Type="fac">100</Attribute>
      <Attribute Type="fac">200</Attribute>
    </Details>'

    SELECT
             Details.Attribute.value('(@Type)[1]', 'varchar(10)') AS 'Type',
             Details.Attribute.value('(.)[1]', 'int') AS 'Value'
          FROM
             @XmlContent.nodes('/Details/Attribute') AS Details(Attribute)
相关问题