在 SQL 中用外部节点包装内部 XML 节点

Wrapping inner XML nodes with an outer node in TSQL

取下面的TSQL

SELECT 
    o.id as '@id',
    (SELECT 
         op.id as '@orderid'
     FROM 
         Orders o
     INNER JOIN 
         OrderParts op ON o.id = op.orderId
     FOR XML PATH ('orderpart'), TYPE)
FROM 
    Orders o
FOR XML PATH ('order'), ROOT('application')

根据这些表格生成。

订单

Id   type
----------------
1    Pending
2    Active

订购零件

id   orderid
-------------
1       1
2       1
3       3
4       2

它生成这个 XML

<application>
  <order id="1">
    <orderpart orderid="1" />
    <orderpart orderid="2" />
    <orderpart orderid="3" />
    <orderpart orderid="4" />
  </order>
  <order id="2">
    <orderpart orderid="1" />
    <orderpart orderid="2" />
    <orderpart orderid="3" />
    <orderpart orderid="4" />
  </order>
</application>

如何使用 TSQL 在 orders 和 orderparts 周围添加外部复数标签,使其看起来如下所示?

<application>
  <orders>
    <order id="1">
      <orderparts>
        <orderpart orderid="1" />
        <orderpart orderid="2" />
        <orderpart orderid="3" />
        <orderpart orderid="4" />
      </orderparts>
    </order>
    <order id="2">
      <orderparts>
        <orderpart orderid="1" />
        <orderpart orderid="2" />
        <orderpart orderid="3" />
        <orderpart orderid="4" />
      </orderparts>
    </order>
  </orders>
</application>

谢谢!

您非常接近 - 只需添加 and/or 调整一些列别名...

SELECT 
    o.id as 'order/@id',
    (SELECT 
         op.id as '@orderid'
     FROM 
         Orders o
     INNER JOIN 
         OrderParts op ON o.id = op.orderId
     FOR XML PATH ('orderpart'), TYPE) AS 'order/orderparts'
FROM 
    Orders o
FOR XML PATH ('orders'), ROOT('application')

应该就可以了! :-)

样本表

CREATE TABLE #Orders(Id INT,[type] VARCHAR(100))

INSERT INTO #Orders
SELECT 1,    'Pending'
UNION ALL
SELECT 2,    'Active'

CREATE TABLE [#Order Parts](Id INT,orderid INT)

INSERT INTO [#Order Parts]
SELECT 1,   1
UNION ALL
SELECT 2,   1
UNION ALL
SELECT 3,   3
UNION ALL
SELECT 4,   2

查询

1.对每个 Id 使用所有可能性(您的预期输出)

;WITH CTE(orderid,orderpartid) AS
(
    SELECT ODR.id orderid ,ODRP.Id orderpartid
    FROM #Orders ODR
    CROSS JOIN [#Order Parts] ODRP 
)
,CTE2 AS
(
   -- This SELECT is used to name the xml data column
    select 
    (
        -- We take orders as the root node
        select t1.orderid as 'order/@Id',
        (
            SELECT T.orderpartid '@orderid' 
            FROM CTE T
            WHERE T.orderid=t1.orderid
            FOR XML PATH('orderpart'), type
        )as 'order/orderparts' 
        from    CTE t1
        group by t1.orderid
        for xml path(''), root('orders')
    )XMLDATA    
)
-- We take application as the root node for orders
select CAST(XMLDATA AS XML)
FROM CTE2
for xml path('application')

结果

2。 Select 每个 Id 的匹配元素

;WITH CTE(orderid,orderpartid) AS
(
    SELECT ODR.id orderid ,ODRP.Id orderpartid
    FROM #Orders ODR
    JOIN [#Order Parts] ODRP ON ODR.id = ODRP.orderId
)
,CTE2 AS
(
    select 
    (
        select t1.orderid as 'order/@Id',
        (
            SELECT T.orderpartid '@orderid' 
            FROM CTE T
            WHERE T.orderid=t1.orderid
            FOR XML PATH('orderpart'), type
        )as 'order/orderparts' 
        from    CTE t1
        group by t1.orderid
        for xml path(''), root('orders')
    )XMLDATA    
)
select CAST(XMLDATA AS XML)
FROM CTE2
for xml path('application')

结果

SELECT 
(
     SELECT o.id AS '@id',
     (
          SELECT op.id as '@orderid'
          FROM OrderParts op 
          WHERE op.orderID = o.ID
          FOR XML PATH ('orderpart'), TYPE
     ) AS 'OrderParts'
     FROM Orders o
     FOR XML PATH ('order'),TYPE
 )
 FOR XML PATH ('orders'),ROOT('application')