1

我有下表:

CREATE TABLE PLMXC_ERP_INT_XML_BOM 
    (
     admi_num int, 
     stlh_num varchar(20),
     arti_oms_1 varchar(40),
     arti_code varchar(20),
     stlx_pos varchar(5),
     stlx_aantal numeric(16,6)
    );

内容如下:

INSERT INTO PLMXC_ERP_INT_XML_BOM
(admi_num, stlh_num, arti_oms_1, arti_code, stlx_pos, stlx_aantal)
VALUES
(1, '3P-2000245', 'Cover 2 undercolum 1MF', '3P-2000242', '1', 1),
(1, '3P-2000245', 'Cover 2 undercolum 1MF', '3P-2000243', '2', 1),
(1, '3P-2000245', 'Cover 2 undercolum 1MF', '3P-101707', '3', 5),
(1, '3P-101707', 'cover 1MF undercolumn', '3P-2000242', '1', 1),
(1, '3P-101707', 'cover 1MF undercolumn', '3P-2000243', '2', 1);

当我想从此内容创建 XML 时,我使用

SELECT  stlh.admi_num
        ,stlh.stlh_num
        ,stlh.arti_oms_1 as stlh_oms_1
        ,(SELECT
            admi_num
            ,stlm.arti_code
            ,ROW_NUMBER()OVER(PARTITION BY stlm.stlh_num ORDER BY stlm.stlh_num) as stlm_num
            ,stlx_pos as stlm_pos
            ,stlx_aantal as stlm_aantal
            FROM PLMXC_ERP_INT_XML_BOM stlm
            WHERE stlm.stlh_num = stlh.stlh_num
            FOR XML AUTO, TYPE, ELEMENTS
        )
        FROM [PLMXC_ERP_INT_XML_BOM] stlh
        GROUP BY stlh.stlh_num, stlh.admi_num, stlh.arti_oms_1
        FOR XML AUTO, ELEMENTS

这让我回来了

<stlh>
  <admi_num>1</admi_num>
  <stlh_num>3P-101707</stlh_num>
  <stlh_oms_1>cover 1MF undercolumn</stlh_oms_1>
  <stlm>
    <admi_num>1</admi_num>
    <arti_code>3P-2000242</arti_code>
    <stlm_num>1</stlm_num>
    <stlm_pos>1</stlm_pos>
    <stlm_aantal>1.000000</stlm_aantal>
  </stlm>
  <stlm>
    <admi_num>1</admi_num>
    <arti_code>3P-2000243</arti_code>
    <stlm_num>2</stlm_num>
    <stlm_pos>2</stlm_pos>
    <stlm_aantal>1.000000</stlm_aantal>
  </stlm>
</stlh>
<stlh>
  <admi_num>1</admi_num>
  <stlh_num>3P-2000245</stlh_num>
  <stlh_oms_1>Cover 2 undercolum 1MF</stlh_oms_1>
  <stlm>
    <admi_num>1</admi_num>
    <arti_code>3P-2000242</arti_code>
    <stlm_num>1</stlm_num>
    <stlm_pos>1</stlm_pos>
    <stlm_aantal>1.000000</stlm_aantal>
  </stlm>
  <stlm>
    <admi_num>1</admi_num>
    <arti_code>3P-2000243</arti_code>
    <stlm_num>2</stlm_num>
    <stlm_pos>2</stlm_pos>
    <stlm_aantal>1.000000</stlm_aantal>
  </stlm>
  <stlm>
    <admi_num>1</admi_num>
    <arti_code>3P-101707</arti_code>
    <stlm_num>3</stlm_num>
    <stlm_pos>3</stlm_pos>
    <stlm_aantal>5.000000</stlm_aantal>
  </stlm>
</stlh>

但我需要但我无法返回的是

<stlh>
  <admi_num>1</admi_num>
  <stlh_num>3P-101707</stlh_num>
  <stlh_oms_1>cover 1MF undercolumn</stlh_oms_1>
</stlh>
<stlm>
  <admi_num>1</admi_num>
  <arti_code>3P-2000242</arti_code>
  <stlm_num>1</stlm_num>
  <stlm_pos>1</stlm_pos>
  <stlm_aantal>1.000000</stlm_aantal>
</stlm>
<stlm>
  <admi_num>1</admi_num>
  <arti_code>3P-2000243</arti_code>
  <stlm_num>2</stlm_num>
  <stlm_pos>2</stlm_pos>
  <stlm_aantal>1.000000</stlm_aantal>
</stlm>

<!-- split into multiple row (result) here -->

<stlh>
  <admi_num>1</admi_num>
  <stlh_num>3P-2000245</stlh_num>
  <stlh_oms_1>Cover 2 undercolum 1MF</stlh_oms_1>
</stlh>
<stlm>
  <admi_num>1</admi_num>
  <arti_code>3P-2000242</arti_code>
  <stlm_num>1</stlm_num>
  <stlm_pos>1</stlm_pos>
  <stlm_aantal>1.000000</stlm_aantal>
</stlm>
<stlm>
  <admi_num>1</admi_num>
  <arti_code>3P-2000243</arti_code>
  <stlm_num>2</stlm_num>
  <stlm_pos>2</stlm_pos>
  <stlm_aantal>1.000000</stlm_aantal>
</stlm>
<stlm>
  <admi_num>1</admi_num>
  <arti_code>3P-101707</arti_code>
  <stlm_num>3</stlm_num>
  <stlm_pos>3</stlm_pos>
  <stlm_aantal>5.000000</stlm_aantal>
</stlm>

我需要将其编写为单独的 XML 文件,对于每个 stlh_num 都需要一个新文件。

这甚至可能吗?

4

1 回答 1

0
select (
       select P1.admi_num,
              P1.stlh_num,
              P1.arti_oms_1
       for xml path('stlh'), type
       ),
       (
       select P2.admi_num,
              P2.arti_code,
              row_number() over(order by P2.stlh_num) as stlm_num,
              P2.stlx_pos as stlm_pos,
              P2.stlx_aantal as stl_aantal
       from PLMXC_ERP_INT_XML_BOM as P2
       where P1.admi_num = P2.admi_num and
             P1.stlh_num = P2.stlh_num and
             P1.arti_oms_1 = P2.arti_oms_1
       for xml path('stlm'), type
       )
from PLMXC_ERP_INT_XML_BOM as P1
group by P1.admi_num,
         P1.stlh_num,
         P1.arti_oms_1
for xml path('')

要将结果拆分为不同的行,您可以改用它。

select (
       select (
              select P1.admi_num,
                     P1.stlh_num,
                     P1.arti_oms_1
              for xml path('stlh'), type
              ),
              (
              select P2.admi_num,
                     P2.arti_code,
                     row_number() over(order by P2.stlh_num) as stlm_num,
                     P2.stlx_pos as stlm_pos,
                     P2.stlx_aantal as stl_aantal
              from PLMXC_ERP_INT_XML_BOM as P2
              where P1.admi_num = P2.admi_num and
                    P1.stlh_num = P2.stlh_num and
                    P1.arti_oms_1 = P2.arti_oms_1
              for xml path('stlm'), type
              )
       for xml path(''), type
       )
from PLMXC_ERP_INT_XML_BOM as P1
group by P1.admi_num,
         P1.stlh_num,
         P1.arti_oms_1
于 2013-11-20T11:37:35.360 回答