Sql 服务器中 select 语句返回的结构化 xml

Structuring xml returned by select statement in Sql Server

比方说,我有两个表

ItemGoods, ServiceGoods 在其他列中有 Name 和 Price。

代表所售商品的不同种类。

我想select把它变成一个xml结构。

现在我正在使用这个select

DECLARE @goods_prices VARCHAR(8000)
SET @goods_prices = (select * from
                        (SELECT  [name] item_name,
                                 [cost] price   
                              FROM  ItemGoods
                        UNION
                        SELECT  [name] service_name,
                                [cost] price
                              FROM  ServiceGoods)   goods
                    FOR XML AUTO);

我得到的输出:

<goods>
  <itemName>Item1</itemName>
  <price>299.0</price>
</goods>
<goods>
  <itemName>Service1</itemName>
  <price>4,99</price>
</goods>

我寻求的输出类似于

<goods>
  <itemGoods>
    <item>
      <itemName>Item1</itemName>
      <price>299.0</price>
    </item>
  </itemGoods>
  <serviceGoods>
    ...
  </serviceGoods>
</goods>

我该如何实现?我需要将结果打包到一个变量中。 一个更简单的 xml 结构就可以了,只要我可以从服务中定义项目

FOR XML EXPLICIT (Use EXPLICIT Mode with FOR XML

declare @ItemGoods table(name varchar(50), cost varchar(50))
declare @ServiceGoods table(name varchar(50), cost varchar(50))

Insert into @ItemGoods(name, cost) values('Item1', '299'), ('Item2', '333')
Insert into @ServiceGoods(name, cost) values('Serv1', '555'),('Serv2', '222')



Select 1 as Tag, NULL as parent
    , NULL as [goods!1!]
    , NULL as [itemGoods!2!]
    , NULL as [item!3!itemName!Element]
    , NULL as [item!3!price!Element]
    , NULL as [serviceGoods!4!]
    , NULL as [item!5!itemName!Element]
    , NULL as [item!5!price!Element]
Union All
Select 2 as Tag, 1 as parent
    , NULL
    , NULL, NULL, NULL
    , NULL, NULL, NULL
Union All
Select 3 as Tag, 2 as parent
    , NULL
    , NULL, [name], [cost]
    , NULL, NULL, NULL
FROM  @ItemGoods item
Union All 
Select 4 as Tag, 1 as parent
    , NULL
    , NULL, NULL, NULL
    , NULL, NULL, NULL
Union All
Select 5 as Tag, 4 as parent
    , NULL
    , NULL, NULL, NULL
    , NULL, [name], [cost]
FROM  @ServiceGoods item
For xml explicit

输出:

<goods>
  <itemGoods>
    <item>
      <itemName>Item1</itemName>
      <price>299</price>
    </item>
    <item>
      <itemName>Item2</itemName>
      <price>333</price>
    </item>
  </itemGoods>
  <serviceGoods>
    <item>
      <itemName>Serv1</itemName>
      <price>555</price>
    </item>
    <item>
      <itemName>Serv2</itemName>
      <price>222</price>
    </item>
  </serviceGoods>
</goods>

你可以使用它(我使用变量 table 是为了方便,但它显然也适用于真实的 tables):

DECLARE @ItemGoods TABLE (
  NAME NVarChar(50) NOT NULL,
  COST Decimal(18, 2) NOT NULL
)

DECLARE @ServiceGoods TABLE (
  NAME NVarChar(50) NOT NULL,
  COST Decimal(18, 2) NOT NULL
)

INSERT INTO @ItemGoods VALUES('Item1', 299.0)
INSERT INTO @ServiceGoods VALUES('Service1', 4.99)

SELECT (
  SELECT NAME AS itemName
   , COST AS price
  FROM @ItemGoods
  FOR XML PATH('Item'), TYPE
) AS ItemGoods
, (
  SELECT NAME AS itemName
   , COST AS price
  FROM @ServiceGoods
  FOR XML PATH('Service'), TYPE
) AS ServiceGoods
FOR XML PATH('Goods')

并且输出:

<Goods>
  <ItemGoods>
    <Item>
      <itemName>Item1</itemName>
      <price>299.00</price>
    </Item>
  </ItemGoods>
  <ServiceGoods>
    <Service>
      <itemName>Service1</itemName>
      <price>4.99</price>
    </Service>
  </ServiceGoods>
</Goods>