2017-10-17 206 views
1

這是我的SQL查詢嵌套XML SQL查詢

SELECT 
    (SELECT TOP 2 
     Docnum AS [PONUMBER], 
     (SELECT FirstName + lastName AS [CreatedBy] 
      FROM OHEM 
      WHERE empID = T0.OwnerCode) AS OwnerCode, 
     T0.CardCode AS [VENDOR_NO], 
     CardName AS [VENDOR_NAME], 
     T0.U_TelFax AS [TELENO], 
     T0.DocDate AS [PODATE], 
     'SPX' AS [COMPCODE], 
     Comments AS [HEAD_TXT], 
     U_ETA AS [EXPDATE], 
     U_No_of_packages AS [ESTIMATEDPACKAGES], 
     REPLACE(REPLACE(T0.Address2, CHAR(10), ''), CHAR(13), '') AS [STREET] 
    FROM 
     OPOR T0  
    ORDER BY 
     CreateDate DESC 
    FOR XML PATH('ZPOHEAD_DATA'), ROOT('POHEAD'), TYPE) POXML 

這是我的輸出:

<POHEAD> 
    <ZPOHEAD_DATA> 
    <PONUMBER>25611</PONUMBER> 
    <OwnerCode>CandyLai</OwnerCode> 
    <VENDOR_NO>V-0410-EUR</VENDOR_NO> 
    <VENDOR_NAME>Robopac S.p.A.</VENDOR_NAME> 
    <PODATE>2017-10-04T00:00:00</PODATE> 
    <COMPCODE>SPX</COMPCODE> 
    <HEAD_TXT>Based On Purchase Request 8315. 8321.</HEAD_TXT> 
    <STREET>23A Tech Park CrescentSingapore 637848</STREET> 
    </ZPOHEAD_DATA> 
    <ZPOHEAD_DATA> 
    <PONUMBER>25612</PONUMBER> 
    <OwnerCode>CandyLai</OwnerCode> 
    <VENDOR_NO>V-0157-SGD</VENDOR_NO> 
    <VENDOR_NAME>Eltete Malaysia Sdn Bhd</VENDOR_NAME> 
    <PODATE>2017-10-04T00:00:00</PODATE> 
    <COMPCODE>SPX</COMPCODE> 
    <HEAD_TXT>*yb mrp</HEAD_TXT> 
    <STREET>15 Joo Koon Road Singapore 628976</STREET> 
    </ZPOHEAD_DATA> 
</POHEAD> 

但我需要的輸出是這樣的 - 請大家幫我修改基於查詢在下面的輸出

<POXML> 
     <POHEAD> 
      <ZPOHEAD_DATA> 
       <PONUMBER>25611</PONUMBER> 
       <OwnerCode>CandyLai</OwnerCode> 
       <VENDOR_NO>V-0410-EUR</VENDOR_NO> 
       <VENDOR_NAME>Robopac S.p.A.</VENDOR_NAME> 
       <PODATE>2017-10-04T00:00:00</PODATE> 
       <COMPCODE>SPX</COMPCODE> 
       <HEAD_TXT>Based On Purchase Request 8315. 8321.</HEAD_TXT> 
       <STREET>23A Tech Park CrescentSingapore 637848</STREET> 
      </ZPOHEAD_DATA> 
     </POHEAD> 
     <POHEAD> 
      <ZPOHEAD_DATA> 
       <PONUMBER>25612</PONUMBER> 
       <OwnerCode>CandyLai</OwnerCode> 
       <VENDOR_NO>V-0157-SGD</VENDOR_NO> 
       <VENDOR_NAME>Eltete Malaysia Sdn Bhd</VENDOR_NAME> 
       <PODATE>2017-10-04T00:00:00</PODATE> 
       <COMPCODE>SPX</COMPCODE> 
       <HEAD_TXT>*yb mrp</HEAD_TXT> 
       <STREET>15 Joo Koon Road Singapore 628976</STREET> 
      </ZPOHEAD_DATA> 
     </POHEAD> 
    </POXML> 
+0

編輯問題添加一些示例數據。 –

回答

1

您需要最後放在「FOR XML PATH('')」。 以下代碼爲

 SELECT 
     (SELECT TOP 2 
      Docnum AS [PONUMBER], 
      (SELECT FirstName + lastName AS [CreatedBy] 
       FROM OHEM 
       WHERE empID = T0.OwnerCode) AS OwnerCode, 
      T0.CardCode AS [VENDOR_NO], 
      CardName AS [VENDOR_NAME], 
      T0.U_TelFax AS [TELENO], 
      T0.DocDate AS [PODATE], 
      'SPX' AS [COMPCODE], 
      Comments AS [HEAD_TXT], 
      U_ETA AS [EXPDATE], 
      U_No_of_packages AS [ESTIMATEDPACKAGES], 
      REPLACE(REPLACE(T0.Address2, CHAR(10), ''), CHAR(13), '') AS [STREET] 
     FROM 
      OPOR T0  
     ORDER BY 
      CreateDate DESC 
     FOR XML PATH('ZPOHEAD_DATA'), ROOT('POHEAD'), TYPE) POXML FOR XML PATH('')