将多行转换为单个XML行。 - SQL Server

时间:2016-10-20 11:20:43

标签: sql-server xml

我有以下格式的一张表。

enter image description here

我希望每个Employee使用XML查询XML格式的数据,如下所示

<Employee>
  <EmployeeID>1</EmployeeID>
  <FirstName>Jon</FirstName>
  <LastName>Snow</LastName>
  <City>North of the wall</City>
  <Age>28</Age>
  <Gender>Male</Male>
</Employee>

<Employee>
  <EmployeeID>2</EmployeeID>
  <FirstName>Arya</FirstName>
  <LastName>Stark</LastName>
  <City>Winterfell</City>
  <Age>14</Age>
  <Gender>Female</Male>
</Employee>

我尝试过使用查询,

SELECT  DISTINCT EmployeeID ,(  
    SELECT   
    EmployeeID,  
    (SELECT    PDFKey ,   
        Replace(Value,char(CAST(0x001A as int)),'') as Value  FROM TempData EE WHERE  E.EmployeeID =EE.EmployeeID FOR XML PATH(''),   
    TYPE, ELEMENTS)   
    AS Employee  
    FROM   
    TempData E WHERE o.EmployeeID =E.EmployeeID  
    GROUP BY EmployeeID  
    FOR XML AUTO, ELEMENTS  
    ) AS XMLtag FROM TempData O 

但它提供的数据格式如

 <E>
 <EmployeeID>1</EmployeeID>
 <Employee>
    <PDFKey>FirstName</PDFKey>
    <Value>Jon</Value>
    <PDFKey>LastName</PDFKey>
    <Value>Snow</Value>
    <PDFKey>City</PDFKey>
    <Value>North of the wall</Value>
    <PDFKey>Age</PDFKey>
    <Value>28</Value>
    <PDFKey>Gender</PDFKey>
    <Value>Male</Value>
  </Employee>
  </E>

我希望每个员工有一行,它有2列,EmployeeID和XMLdata。

请参考下面的图片了解预期输出, enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用PIVOT + FOR XML PATH:

SELECT  *
FROM Employee
PIVOT (
    MAX([Value]) FOR PDFKey IN ([FirstName],[LastName],[City],[Age],[Gender])
) as u
FOR XML PATH('Employee'), TYPE

输出:

<Employee>
  <EmployeeID>1</EmployeeID>
  <FirstName>Jon</FirstName>
  <LastName>Snow</LastName>
  <City>North of the wall</City>
  <Age>28</Age>
  <Gender>Male</Gender>
</Employee>
<Employee>
  <EmployeeID>2</EmployeeID>
  <FirstName>Arya</FirstName>
  <LastName>Stark</LastName>
  <City>Winterfell</City>
  <Age>14</Age>
  <Gender>Female</Gender>
</Employee>

转动后你会得到这样的表:

EmployeeID  FirstName   LastName    City                Age Gender
1           Jon         Snow        North of the wall   28  Male
2           Arya        Stark       Winterfell          14  Female

之后,添加FOR XML with PATH mode

  

在PATH模式下,列名或列别名被视为XPath   表达式。这些表达式表明了值的来源   映射到XML。每个XPath表达式都是一个提供的相对XPath   项类型。,例如属性,元素和标量值,以及   将生成的节点的名称和层次结构   行元素。

我也添加了TYPE

  

SQL Server对xml(Transact-SQL)的支持使您能够   可选地请求返回FOR XML查询的结果   xml数据类型,通过指定TYPE指令。

修改

;WITH cte AS (
SELECT  *
FROM Employee
PIVOT (
    MAX([Value]) FOR PDFKey IN ([FirstName],[LastName],[City],[Age],[Gender])
) as u
)

SELECT  e.EmployeeID,
        (SELECT *
        FROM cte
        WHERE EmployeeID = e.EmployeeID
        FOR XML PATH('Employee'), TYPE) as XMLData
FROM cte e

输出:

EmployeeID  XMLData
1           <Employee><EmployeeID>1</EmployeeID><FirstName>Jon</FirstName><LastName>Snow</LastName><City>North of the wall</City><Age>28</Age><Gender>Male</Gender></Employee>
2           <Employee><EmployeeID>2</EmployeeID><FirstName>Arya</FirstName><LastName>Stark</LastName><City>Winterfell</City><Age>14</Age><Gender>Female</Gender></Employee>