在FOR XML结果中需要多个标签

时间:2017-10-22 17:34:10

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

我尝试输出XML文本,其中输出行有多个根标记,选择结果也有多个根标记。我理解这有点模糊,所以让我试着说明问题。 真实环境相当复杂,所以我试图简化这个结构。

在这个简化示例的情况下,涉及2个表。一个名为xml_orderlines,另一个名为xml_clients。 表结构如下:

对于1个客户,有1个或更多订单。我知道我们应该在这个结构中也有一个表订单,但是对于这个例子它不适用)

创建xml_orderlines:

CREATE TABLE [dbo].[xml_OrderLines](
[orderid] [int] NULL,
[cl_id] [int] NULL,
[DtOrder] [date] NULL,
[PlcOfOrder] [varchar](50) NULL,
[Accman] [varchar](50) NULL,
[Art_id] [int] NULL
) ON [PRIMARY]

GO

INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (12,101,'2017-05-31','Rotterdam',201,301)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (13,102,'2017-06-15','Amsterdam',202,302)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (14,103,'2017-07-15','London',203,303)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (15,104,'2017-08-15','Dublin',204,304)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (16,101,'2017-05-31','Rotterdam',205,305)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (17,102,'2017-06-15','Amsterdam',206,306)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (18,103,'2017-07-15','London',207,307)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (19,104,'2017-08-15','Dublin',208,308)
INSERT INTO xml_OrderLines (orderid,cl_id,DtOrder,PlcOfOrder,Accman,art_id) VALUES (18,103,'2017-07-15','London',207,307)

创建xml_cients:

CREATE TABLE [dbo].[xml_clients](
[cl_id] [int] NULL,
[name] [varchar](50) NULL,
[surname] [varchar](50) NULL,
[DtOfBirth] [date] NULL
) ON [PRIMARY]

GO

INSERT INTO xml_Clients  (cl_id,name,surname,DtOfBirth) VALUES (101,'Jan','Winter','2000-05-31')
INSERT INTO xml_Clients  (cl_id,name,surname,DtOfBirth) VALUES (102,'John','Summer','2000-06-15')
INSERT INTO xml_Clients  (cl_id,name,surname,DtOfBirth) VALUES (103,'Jean','Autum','2000-05-12')
INSERT INTO xml_Clients  (cl_id,name,surname,DtOfBirth) VALUES (104,'Janis','Spring','2000-06-16')

这是我运行的查询:

SELECT 
ol.Art_id   AS 'ArtikelOrder'
,
    (
    SELECT
        c.cl_id AS 'Buyer/Client'
    ,   c.name  AS 'Buyer/FirstName'
    ,   c.surname   AS 'Buyer/SurName'
    ,   c.DtOfBirth AS 'Buyer/DateOfBirth'
    from 
        xml_clients AS c
    join
        xml_orderlines AS x ON c.cl_id=x.cl_id

    WHERE x.art_id = ol.art_id
    FOR XML PATH(''), TYPE, ROOT('Clients')
    )
,   ol.DtOrder  AS 'OrderDate'
,   ol.plcOfOrder AS 'OrderPlace'
,   ol.orderid AS 'OrderId'
FROM
xml_orderlines AS ol 
FOR XML PATH('Tx'),TYPE, ROOT('Document')

此查询以XML格式提供以下结果:

<Document>
 <Tx>
  <ArtikelOrder>307</ArtikelOrder>
  <Clients>
    <Buyer>
      <Client>103</Client>
      <FirstName>Jean</FirstName>
      <SurName>Autum</SurName>
      <DateOfBirth>2000-05-12</DateOfBirth>
    </Buyer>
    <Buyer>
      <Client>103</Client>
      <FirstName>Jean</FirstName>
      <SurName>Autum</SurName>
      <DateOfBirth>2000-05-12</DateOfBirth>
    </Buyer>
  </Clients>
  <OrderDate>2017-07-15</OrderDate>
  <OrderPlace>London</OrderPlace>
  <OrderId>18</OrderId>
 </Tx>
 <Tx>
   <ArtikelOrder>302</ArtikelOrder>
    <Clients>
    <Buyer>
      <Client>102</Client>
      <FirstName>John</FirstName>
      <SurName>Summer</SurName>
      <DateOfBirth>2000-06-15</DateOfBirth>
    </Buyer>
   </Clients>
 <OrderDate>2017-06-15</OrderDate>
 <OrderPlace>Amsterdam</OrderPlace>
 <OrderId>13</OrderId>
</Tx>
</Document>

我尝试实现以下输出:

<FirstQRoot>
 <Document>
  <FirstRowRoot>
   <Tx>
    <ArtikelOrder>307</ArtikelOrder>
    <Clients>
     <Buyer>
      <Client>103</Client>
      <FirstName>Jean</FirstName>
      <SurName>Autum</SurName>
      <DateOfBirth>2000-05-12</DateOfBirth>
     </Buyer>
     <Buyer>
      <Client>103</Client>
      <FirstName>Jean</FirstName>
      <SurName>Autum</SurName>
      <DateOfBirth>2000-05-12</DateOfBirth>
     </Buyer>
    </Clients>
    <OrderDate>2017-07-15</OrderDate>
    <OrderPlace>London</OrderPlace>
    <OrderId>18</OrderId>
   </Tx>
  </FirstRowRoot>
  <FirstRowRoot>
   <Tx>
   <ArtikelOrder>302</ArtikelOrder>
    <Clients>
     <Buyer>
      <Client>102</Client>
      <FirstName>John</FirstName>
      <SurName>Summer</SurName>
      <DateOfBirth>2000-06-15</DateOfBirth>
     </Buyer>
    </Clients>
    <OrderDate>2017-06-15</OrderDate>
    <OrderPlace>Amsterdam</OrderPlace>
    <OrderId>13</OrderId>
   </Tx>
  </FirstRowRoot>
 </Document>
</FirstQRoot>

我希望可以添加“FirstQRoot”和“FirstRowRoot” 标签。 我已经尝试了几个子查询和双标签组合(标签名称中有/),但没有一个解决了这个问题。

2 个答案:

答案 0 :(得分:1)

添加内部两个嵌套的行根有点棘手......尝试这样:

{{1}}

答案 1 :(得分:0)

演示 初步查询:

select x,y
from (
    --demo data
     select 1 as x, 10 as y
     union all
     select 2, 20
) t 
for xml path('Tx'), type, root ('Document');

返回

<Document>
  <Tx>
    <x>1</x>
    <y>10</y>
  </Tx>
  <Tx>
    <x>2</x>
    <y>20</y>
  </Tx>
</Document>

重构查询:

select Document
from (
    select x as [Tx/x],y as [Tx/y] 
      from (
         --demo data
         select 1 as x, 10 as y
         union all
         select 2, 20) t 
    for xml path('FirstRowRoot'), type
) t2(Document)
for xml path(''), type, root('FirstQRoot');

使用更新后的问题数据:

SELECT Document
FROM(
    SELECT 
    ol.Art_id   AS [Tx/ArtikelOrder]
    ,(
        SELECT
            c.cl_id AS 'Buyer/Client'
        ,   c.name  AS 'Buyer/FirstName'
        ,   c.surname   AS 'Buyer/SurName'
        ,   c.DtOfBirth AS 'Buyer/DateOfBirth'
        from 
            xml_clients AS c
        join
            xml_orderlines AS x ON c.cl_id=x.cl_id

        WHERE x.art_id = ol.art_id
        FOR XML PATH(''), TYPE
     ) AS [Tx/Clients]
    ,   ol.DtOrder  AS [Tx/OrderDate]
    ,   ol.plcOfOrder AS [Tx/OrderPlace]
    ,   ol.orderid AS [Tx/OrderId]
    FROM
    xml_orderlines AS ol 
    FOR XML PATH('FirstRowRoot'),TYPE
) t(Document)
FOR XML PATH(''), TYPE, ROOT('FirstQRoot');