首页 > 解决方案 > XML 粉碎动态 SQL

问题描述

使用此处第一个答案的说明,我试图在 SQL Server 2012 表中分解一些 XML,如下所示:

表A

+------+--------+
|  ID  | ColXML |
+------+--------+
| 0001 | <xml1> |
| 0002 | <xml2> |
| ...  | ...    |
+------+--------+

xml1 看起来像这样:

<Attributes>
    <Attribute name="address1">301 Main St</Attribute>
    <Attribute name="city">Austin</Attribute>
</Attributes>

xml2 看起来像这样:

<Attributes>
    <Attribute name="address1">501 State St</Attribute>
    <Attribute name="address2">Suite 301</Attribute>
    <Attribute name="state">Texas</Attribute>
</Attributes>

在任何给定的行中都有不同数量的属性。

我正在尝试将其展平为如下所示的关系表:

+------+--------------+-----------+--------+-------+
|  ID  |   address1   | address2  |  city  | state |
+------+--------------+-----------+--------+-------+
| 0001 | 301 Main St  | NULL      | Austin | NULL  |
| 0002 | 501 State St | Suite 301 | NULL   | Texas |
+------+--------------+-----------+--------+-------+

这是我尝试过的代码,它在表#T 中返回 0 行:

select dense_rank() over(order by ID, I.N) as ID,
       F.N.value('(*:Name/text())[1]', 'varchar(max)') as Name,
       F.N.value('(*:Values/text())[1]', 'varchar(max)') as Value
into #T
from TableA as T
  cross apply T.Attributes.nodes('/ColXML') as I(N)
  cross apply I.N.nodes('ColXML') as F(N);

declare @SQL nvarchar(max)
declare @Col nvarchar(max);

select @Col = 
  (
  select distinct ','+quotename(Name)
  from #T
  for xml path(''), type
  ).value('substring(text()[1], 2)', 'nvarchar(max)');

set @SQL = 'select '+@Col+'
            from #T
            pivot (max(Value) for Name in ('+@Col+')) as P';

exec (@SQL);

任何帮助将不胜感激。

标签: sqlsql-serverxmlxquery

解决方案


这是用于分解表中的 XML的 DDL 和XQuery 。不需要任何动态 SQL。MS SQL Server 支持XQuery 1.0 标准的一个子集。Microsoft 需要实施 XQuery 3.1 以使其数据库更加强大。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID VARCHAR(10) PRIMARY KEY, ColXML XML);
INSERT INTO @tbl (ID, ColXML)
VALUES
('0001', N'<Attributes>
    <Attribute name="address1">301 Main St</Attribute>
    <Attribute name="city">Austin</Attribute>
</Attributes>'),
('0002', N'<Attributes>
    <Attribute name="address1">501 State St</Attribute>
    <Attribute name="address2">Suite 301</Attribute>
    <Attribute name="state">Texas</Attribute>
</Attributes>');
-- DDL and sample data population, end

SELECT ID
    , col.value('(Attribute[@name="address1"]/text())[1]','VARCHAR(30)') AS [address1]
    , col.value('(Attribute[@name="address2"]/text())[1]','VARCHAR(30)') AS [address2]
    , col.value('(Attribute[@name="city"]/text())[1]','VARCHAR(30)') AS [city]
    , col.value('(Attribute[@name="state"]/text())[1]','VARCHAR(30)') AS [state]
FROM @tbl AS tbl
    CROSS APPLY tbl.ColXML.nodes('/Attributes') AS tab(col);

输出

+------+--------------+-----------+--------+-------+
|  ID  |   address1   | address2  |  city  | state |
+------+--------------+-----------+--------+-------+
| 0001 | 301 Main St  | NULL      | Austin | NULL  |
| 0002 | 501 State St | Suite 301 | NULL   | Texas |
+------+--------------+-----------+--------+-------+

推荐阅读