我正在尝试使用以下格式构建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 +上实现这一点的想法吗?
答案 0 :(得分:1)
我简化了你的查询。假设您的查询如下所示:
SELECT STR(X) AS [office/schedule]
FROM (VALUES(1),(2)) AS CompleQuery(x)
FOR XML PATH('Submission'),TYPE;
产生:
<Submission><office><schedule> 1</schedule></office></Submission><Submission><office><schedule> 2</schedule></office></Submission>
要将两个办公室代码添加到一个提交中,请使用此代码:
SELECT (
SELECT STR(X) AS [office/schedule]
FROM (VALUES(1),(2)) AS CompleQuery(x)
FOR XML PATH(''),TYPE
)
FOR XML PATH('Submission'),TYPE;
因此,请在查询中将PATH('Submission')
更改为PATH('')
,然后将其换入另一个SELECT ... FOR XML PATH('Submission'),TYPE;
结果:
<Submission><office><schedule> 1</schedule></office><office><schedule> 2</schedule></office></Submission>