使用 SQL 将 XML 结构转置/展平到列

Using SQL to transpose/flatten XML structure to columns(使用 SQL 将 XML 结构转置/展平到列)
本文介绍了使用 SQL 将 XML 结构转置/展平到列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Server (2008/2012),并且我知道从大量搜索中得到了类似的答案,但是我似乎无法找到适合我的案例的示例/指针.

I am using SQL Server (2008/2012) and I know there are similar answers from lots of searching, however I can't seem to find the appropriate example/pointers for my case.

我在 SQL Server 表中有一个 XML 列,其中包含这些数据:

I have an XML column in a SQL Server table holding this data:

<Items>
 <Item>
  <FormItem>
    <Text>FirstName</Text>
    <Value>My First Name</Value>
  </FormItem>
  <FormItem>
    <Text>LastName</Text>
    <Value>My Last Name</Value>
  </FormItem>
  <FormItem>
    <Text>Age</Text>
    <Value>39</Value>
  </FormItem>
 </Item>
 <Item>
  <FormItem>
    <Text>FirstName</Text>
    <Value>My First Name 2</Value>
  </FormItem>
  <FormItem>
    <Text>LastName</Text>
    <Value>My Last Name 2</Value>
  </FormItem>
  <FormItem>
    <Text>Age</Text>
    <Value>40</Value>
  </FormItem>
 </Item>
</Items>

所以即使 <FormItem> 的结构是相同的,我也可以拥有多组(通常不超过 20-30 个)表单项..

So even though the structure of <FormItem> is going to be the same, I can have multiple (most commonly no more than 20-30) sets of form items..

我实际上是在尝试以以下格式从 SQL 返回查询,即基于/FormItem/Text 的动态列:

I am essentially trying to return a query from SQL in the format below, i.e. dynamic columns based on /FormItem/Text:

FirstName         LastName         Age    ---> More columns as new `<FormItem>` are returned
My First Name     My Last Name     39          Whatever value etc..
My First Name 2   My Last Name 2   40          

所以,此刻我有以下几点:

So, at the moment I had the following:

select 
    Tab.Col.value('Text[1]','nvarchar(100)') as Question,
    Tab.Col.value('Value[1]','nvarchar(100)') as Answer
from
    @Questions.nodes('/Items/Item/FormItem') Tab(Col)

当然,这并没有将我的 XML 行转换为列,而且显然是用字段固定的.我一直在尝试各种动态 SQL"方法,其中 SQL 执行不同的选择(在我的情况下)<Text> 节点,然后使用某种 Pivot?但我似乎找不到神奇的组合来返回我需要的结果作为每一行的动态列集(<Items><集合中的 /code>).

Of course that hasn't transposed my XML rows into columns, and obviously is fixed with fields anyway.. I have been trying various "Dynamic SQL" approaches where the SQL performs a distinct selection of (in my case) the <Text> node, and then uses some sort of Pivot? but I couldn't seem to find the magic combination to return the results I need as a dynamic set of columns for each row (<Item> within the collection of <Items>).

我相信看到这么多非常相似的例子是可以做到的,但是我又找不到解决方案!

I'm sure it can be done having seen so many very similar examples, however again the solution eludes me!

感谢您的帮助!

推荐答案

解析 XML 相当昂贵,因此您可以创建一个具有名称-值的临时表,而不是解析一次来构建动态查询和一次获取数据列表,然后将其用作动态数据透视查询的来源.
dense_rank 用于创建 ID 以进行旋转.
为了在动态查询中构建列列表,它使用 for xml path('') 技巧.

Parsing the XML is fairly expensive so instead of parsing once to build a dynamic query and once to get the data you can create a temporary table with a Name-Value list and then use that as the source for a dynamic pivot query.
dense_rank is there to create the ID to pivot around.
To build the column list in the dynamic query it uses the for xml path('') trick.

此解决方案要求您的表具有主键 (ID).如果您在变量中包含 XML,则可以稍微简化一下.

This solution requires that your table has a primary key (ID). If you have the XML in a variable it can be somewhat simplified.

select dense_rank() over(order by ID, I.N) as ID,
       F.N.value('(Text/text())[1]', 'varchar(max)') as Name,
       F.N.value('(Value/text())[1]', 'varchar(max)') as Value
into #T
from YourTable as T
  cross apply T.XMLCol.nodes('/Items/Item') as I(N)
  cross apply I.N.nodes('FormItem') 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)

drop table #T

SQL 小提琴

这篇关于使用 SQL 将 XML 结构转置/展平到列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)