我正在尝试以以下格式构建 XML(我们的提供程序没有 XSD):
<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+ 上实现这一点有什么想法吗?