0
我試圖建立在以下格式的XML(我們的供應商有沒有XSD):TSQL - 查詢XML和合並根節點
<Submission>
<Office Account="ACCOUNT">
<schedule submissionPeriod="" areaOfLaw="" scheduleNum="abc">
<outcome matterType="A:B">
<outcomeItem Name="CASE_START_DATE">DATA</outcomeItem>
<outcomeItem Name="CASE_ID">DATA</outcomeItem>
</outcome>
<outcome matterType="C:D">
<outcomeItem Name="CASE_START_DATE">DATA</outcomeItem>
<outcomeItem Name="CASE_ID">DATA</outcomeItem>
</outcome>
</schedule>
<schedule submissionPeriod="" areaOfLaw="" scheduleNum="def">
<outcome matterType="E:F">
<outcomeItem Name="CASE_START_DATE">DATA</outcomeItem>
<outcomeItem Name="CASE_ID">DATA</outcomeItem>
</outcome>
</schedule>
</Office>
<Office Account="ACCOUNT2">
<schedule submissionPeriod="" areaOfLaw="" scheduleNum="ghi">
<outcome matterType="A:B">
<outcomeItem Name="CASE_START_DATE">DATA</outcomeItem>
<outcomeItem Name="CASE_ID">DATA</outcomeItem>
</outcome>
</schedule>
</Office>
我有以下SQL
SELECT supplier_no AS 'Office/@Account' ,
UPPER(LEFT(DATENAME(MM, GETDATE()), 3)) + '-' + DATENAME(YYYY,
GETDATE()) AS 'Office/schedule/@submissionPeriod' ,
'LEGAL HELP' AS 'Office/schedule/@areaOfLaw' ,
supplier_no + '/CIVIL' AS 'Office/schedule/@scheduleNum' ,
ClaimsTable.mattype1_code + ':' + ClaimsTable.mattype2_code AS 'Office/schedule/outcome/@matterType' ,
(SELECT * FROM (
SELECT 'CASE_START_DATE' AS 'outcomeItem/@Name', 'DATA' AS 'outcomeItem' UNION ALL
SELECT 'CASE_ID' AS 'outcomeItem/@Name', 'DATA' AS 'outcomeItem'
) OutcomeItems FOR XML PATH(''), TYPE) AS 'Office/schedule/outcome'
FROM ContractsTable
LEFT OUTER JOIN ClaimsTable ON ContractsTable.contract_no = ClaimsTable.contract_no
WHERE ClaimsTable.compno = 2
AND ClaimsTable.submitted_period = 0
FOR XML PATH('Submission') ,
TYPE
這是產生下面的XML
<Submission>
<Office Account="MHESUPPLIE">
<schedule submissionPeriod="FEB-2013" areaOfLaw="LEGAL HELP" scheduleNum="MHESUPPLIE/CIVIL">
<outcome matterType="MHNS:MOUT">
<outcomeItem Name="CASE_START_DATE">13/02/2013</outcomeItem>
<outcomeItem Name="CASE_ID">001</outcomeItem>
</outcome>
</schedule>
</Office>
</Submission>
<Submission>
<Office Account="MHESUPPLIE">
<schedule submissionPeriod="FEB-2013" areaOfLaw="LEGAL HELP" scheduleNum="MHESUPPLIE/CIVIL">
<outcome matterType="MHAS:MGUA">
<outcomeItem Name="CASE_START_DATE">14/02/2013</outcomeItem>
<outcomeItem Name="CASE_ID">001</outcomeItem>
</outcome>
</schedule>
</Office>
</Submission>
顯然,輸出XML有多個根節點是無效。我試圖找到一種方法將結果組合在一起以實現我需要的XML類型。有沒有關於SQL Server 2005+如何實現的想法?
如果使用FOR XML PATH(''),ROOT('Submission')'會發生什麼? – 2013-02-14 16:30:06