在SQL Server 2014中使用多个子节点创建XML输出

时间:2018-05-30 16:13:00

标签: sql sql-server xml sql-server-2014

我正在尝试使用SQL创建XML输出。我可以使用基本列表来完成它,但不能使用列表中的列表。基本结构:

CREATE TABLE #TEMP1 (   ID1     INT
                        ,Names VARCHAR(10)
                        ,Address VARCHAR(50)
                )

Create Table #TEMP2 ( ID2 INT
                        ,ID1 INT
                        ,ITEM VARCHAR(10)
                        ,Quantity INT
                    )

INSERT INTO #TEMP1 (ID1,Names,Address)
VALUES (1,'Jack','Main St')
        ,(2,'Jill','Second St')
        ,(3,'Hill','3rd St')

INSERT INTO #TEMP2(ID2,ID1,ITEM,Quantity)
Values (1,1,'Curds',20)
        ,(2,2,'Way',30)
        ,(3,2,'Curds',40)
        ,(4,3,'Curds',50)
        ,(5,3,'Curds',60)
        ,(6,3,'Curds',70)

当我运行以下XML时,我得到了所需的结果:

SELECT ID1      CusID
        ,Names  CusName
        ,Address    PrimAddress
FROM #TEMP1 Customer
FOR XML AUTO, ELEMENTS

<Customer>
    <CusID>1</CusID>
    <CusName>Jack</CusName>
    <PrimAddress>Main St</PrimAddress>
</Customer>
<Customer>
    <CusID>2</CusID>
    <CusName>Jill</CusName>
    <PrimAddress>Second St</PrimAddress>
</Customer>
<Customer>
    <CusID>3</CusID>
    <CusName>Hill</CusName>
    <PrimAddress>3rd St</PrimAddress>
</Customer>

当我尝试将订单(#TEMP2)添加到混合中时,会出现问题。

SELECT ID1      CusID
        ,Names  CusName
        ,Address    PrimAddress
        ,(SELECT ID2 PRODUCTID
                ,ITEM   PRODUCTNAME
                ,Quantity
            FROM #TEMP2 Items
            where ID1 = Customer.ID1
            FOR XML AUTO, ELEMENTS)
FROM #TEMP1 Customer
FOR XML AUTO, ELEMENTS

OutPut有一堆特殊的字符符号,而不是干净的XML:

 <Customer>
    <CusID>1</CusID>
    <CusName>Jack</CusName>
    <PrimAddress>Main St</PrimAddress>&lt;Items&gt;&lt;PRODUCTID&gt;1&lt;/PRODUCTID&gt;&lt;PRODUCTNAME&gt;Curds&lt;/PRODUCTNAME&gt;&lt;Quantity&gt;20&lt;/Quantity&gt;&lt;/Items&gt;</Customer>
<Customer>
    <CusID>2</CusID>
    <CusName>Jill</CusName>
    <PrimAddress>Second St</PrimAddress>&lt;Items&gt;&lt;PRODUCTID&gt;2&lt;/PRODUCTID&gt;&lt;PRODUCTNAME&gt;Way&lt;/PRODUCTNAME&gt;&lt;Quantity&gt;30&lt;/Quantity&gt;&lt;/Items&gt;&lt;Items&gt;&lt;PRODUCTID&gt;3&lt;/PRODUCTID&gt;&lt;PRODUCTNAME&gt;Curds&lt;/PRODUCTNAME&gt;&lt;Quantity&gt;40&lt;/Quantity&gt;&lt;/Items&gt;</Customer>
<Customer>
    <CusID>3</CusID>
    <CusName>Hill</CusName>
    <PrimAddress>3rd St</PrimAddress>&lt;Items&gt;&lt;PRODUCTID&gt;4&lt;/PRODUCTID&gt;&lt;PRODUCTNAME&gt;Curds&lt;/PRODUCTNAME&gt;&lt;Quantity&gt;50&lt;/Quantity&gt;&lt;/Items&gt;&lt;Items&gt;&lt;PRODUCTID&gt;5&lt;/PRODUCTID&gt;&lt;PRODUCTNAME&gt;Curds&lt;/PRODUCTNAME&gt;&lt;Quantity&gt;60&lt;/Quantity&gt;&lt;/Items&gt;&lt;Items&gt;&lt;PRODUCTID&gt;6&lt;/PRODUCTID&gt;&lt;PRODUCTNAME&gt;Curds&lt;/PRODUCTNAME&gt;&lt;Quantity&gt;70&lt;/Quantity&gt;&lt;/Items&gt;</Customer>

期望的结果如下所示。可能不是完美的XML,但重点是删除所有特殊字符(不要尝试替换或类似的东西):

<Customer>
    <CusID>1</CusID>
    <CusName>Jack</CusName>
    <PrimAddress>Main St</PrimAddress>
        <Items>
            <PRODUCTID>1</PRODUCTID>
            <PRODUCTNAME>Curds</PRODUCTNAME>
            <Quantity>20</Quantity>
        </Items>
</Customer>
<Customer>
    <CusID>2</CusID>
    <CusName>Jill</CusName>
    <PrimAddress>Second St</PrimAddress>
    <Items>
        <Item></Item>
        <PRODUCTID>2</PRODUCTID>
        <PRODUCTNAME>Way</PRODUCTNAME>
        <Quantity>30</Quantity>
        </Item>
    </Items>
    <Items>
        <PRODUCTID>3</PRODUCTID>
        <PRODUCTNAME>Curds</PRODUCTNAME>
        <Quantity>40</Quantity>
    </Items>
</Customer>

2 个答案:

答案 0 :(得分:0)

本来希望看到它添加在循环项目的逻辑中,但我认为这是有效的。

Select 
(SELECT ID1     CusID
        ,Names  CusName
        ,Address    PrimAddress
        ,(SELECT ID2 PRODUCTID
                ,ITEM   PRODUCTNAME
                ,Quantity
            FROM #TEMP2 Items
            where ID1 = Customer.ID1
            FOR XML PATH(''), TYPE, ELEMENTS) AS Items
FROM #TEMP1 Customer
FOR XML AUTO, ELEMENTS) 

也非常感谢:https://www.codeproject.com/Articles/54584/Controlling-the-XML-output-when-using-the-FOR-XML

答案 1 :(得分:0)

我认为你正在寻找:

CREATE TABLE #TEMP1 (    ID1     INT
                        ,Names VARCHAR(10)
                        ,Address VARCHAR(50)
                )

Create Table #TEMP2 ( ID2 INT
                     ,ID1 INT
                     ,ITEM VARCHAR(10)
                     ,Quantity INT
                    )

INSERT INTO #TEMP1 (ID1,Names,Address)
VALUES (1,'Jack','Main St')
        ,(2,'Jill','Second St')
        ,(3,'Hill','3rd St')

INSERT INTO #TEMP2(ID2,ID1,ITEM,Quantity)
Values (1,1,'Curds',20)
        ,(2,2,'Way',30)
        ,(3,2,'Curds',40)
        ,(4,3,'Curds',50)
        ,(5,3,'Curds',60)
        ,(6,3,'Curds',70);

SELECT ID1 AS CusID
      ,Names AS CusName
      ,[Address] AS PrimAddress
      ,(SELECT items.ID2 AS PRODUCTID
              ,items.ITEM AS PRODUCTNAME
              ,items.Quantity
        FROM #TEMP2 Items
        WHERE items.ID1 = Customer.ID1
        FOR XML PATH('Item'),ROOT('Items'), TYPE) AS [*]
FROM #TEMP1 Customer
FOR XML PATH('Customer'),ROOT('Customers');

结果

<Customers>
  <Customer>
    <CusID>1</CusID>
    <CusName>Jack</CusName>
    <PrimAddress>Main St</PrimAddress>
    <Items>
      <Item>
        <PRODUCTID>1</PRODUCTID>
        <PRODUCTNAME>Curds</PRODUCTNAME>
        <Quantity>20</Quantity>
      </Item>
    </Items>
  </Customer>
  <Customer>
    <CusID>2</CusID>
    <CusName>Jill</CusName>
    <PrimAddress>Second St</PrimAddress>
    <Items>
      <Item>
        <PRODUCTID>2</PRODUCTID>
        <PRODUCTNAME>Way</PRODUCTNAME>
        <Quantity>30</Quantity>
      </Item>
      <Item>
        <PRODUCTID>3</PRODUCTID>
        <PRODUCTNAME>Curds</PRODUCTNAME>
        <Quantity>40</Quantity>
      </Item>
    </Items>
  </Customer>
  <Customer>
    <CusID>3</CusID>
    <CusName>Hill</CusName>
    <PrimAddress>3rd St</PrimAddress>
    <Items>
      <Item>
        <PRODUCTID>4</PRODUCTID>
        <PRODUCTNAME>Curds</PRODUCTNAME>
        <Quantity>50</Quantity>
      </Item>
      <Item>
        <PRODUCTID>5</PRODUCTID>
        <PRODUCTNAME>Curds</PRODUCTNAME>
        <Quantity>60</Quantity>
      </Item>
      <Item>
        <PRODUCTID>6</PRODUCTID>
        <PRODUCTNAME>Curds</PRODUCTNAME>
        <Quantity>70</Quantity>
      </Item>
    </Items>
  </Customer>
</Customers>