这是我的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)
您需要最后放置&#34; FOR XML PATH(&#39;&#39;)&#34;。 请使用以下代码
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('')