2013-02-14 81 views
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+如何實現的想法?

+1

如果使用FOR XML PATH(''),ROOT('Submission')'會發生什麼? – 2013-02-14 16:30:06

回答

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>