2017-08-04 115 views
1

鑑於這些表:從SQL Server檢索複雜的XML 2016

DECLARE @Documents TABLE 
(
    document_id int, 
    document_file varchar(200), 
    description varchar(200), 
    pages int, 
    write_barcode bit 
) 

DECLARE @Data TABLE 
(
    id int, 
    tax_id varchar(100), 
    bo_lgl_name varchar(200), 
    document_id int, 
    keydata varchar(100), 
    field_name varchar(100), 
    value varchar(100) 
) 

填充了這樣的數據:

INSERT INTO @Data VALUES (1, '123456789', 'Fred Flintstone', 269, Null, 'LastName', 'Smith') 
INSERT INTO @Data VALUES (1, '123456789', 'Fred Flintstone', 269, Null, 'FirstName', 'Joe') 
INSERT INTO @Data VALUES (1, '123456789', 'Fred Flintstone', 6, Null, 'TaxID', '123456789') 
INSERT INTO @Data VALUES (1, '123456789', 'Fred Flintstone', 6, Null, 'Address', 'New York') 

INSERT INTO @Data VALUES (2, '666666666', 'Barney Rubble', 269, Null, 'LastName', 'Jones') 
INSERT INTO @Data VALUES (2, '666666666', 'Barney Rubble', 269, Null, 'FirstName', 'Fred') 
INSERT INTO @Data VALUES (2, '666666666', 'Barney Rubble', 6, Null, 'TaxID', '987654321') 
INSERT INTO @Data VALUES (2, '666666666', 'Barney Rubble', 6, Null, 'Address', 'Los Angeles') 

INSERT INTO @Documents VALUES (269, '8802.pdf', Null, Null, Null) 
INSERT INTO @Documents VALUES (6, 'Doclist.xsl', Null, Null, Null) 

我想檢索該XML:

<Documents> 
    <Batch BatchID="1" BatchName="Fred Flintstone"> 
    <DocCollection> 
     <Document DocumentID="269" FileName="8802.pdf" KeyData=""> 
     <MergeFields> 
      <MergeField FieldName="LastName" Value="Smith" /> 
      <MergeField FieldName="LastName" Value="Joe" /> 
      <MergeField FieldName="TaxID" Value="123456789" /> 
      <MergeField FieldName="Address" Value="New York" /> 
     </MergeFields> 
     </Document> 
     <Document DocumentID="6" FileName="Doclist.xsl" KeyData=""> 
     <MergeFields> 
      <MergeField FieldName="LastName" Value="Smith" /> 
      <MergeField FieldName="LastName" Value="Joe" /> 
      <MergeField FieldName="TaxID" Value="123456789" /> 
      <MergeField FieldName="Address" Value="New York" /> 
     </MergeFields> 
     </Document> 
    </DocCollection> 
    </Batch> 
    <Batch BatchID="2" BatchName="Barney Rubble"> 
    <DocCollection> 
     <Document DocumentID="269" FileName="8802.pdf" KeyData=""> 
     <MergeFields> 
      <MergeField FieldName="LastName" Value="Smith" /> 
      <MergeField FieldName="LastName" Value="Joe" /> 
      <MergeField FieldName="TaxID" Value="123456789" /> 
      <MergeField FieldName="Address" Value="New York" /> 
     </MergeFields> 
     </Document> 
     <Document DocumentID="6" FileName="Doclist.xsl" KeyData=""> 
     <MergeFields> 
      <MergeField FieldName="LastName" Value="Smith" /> 
      <MergeField FieldName="LastName" Value="Joe" /> 
      <MergeField FieldName="TaxID" Value="123456789" /> 
      <MergeField FieldName="Address" Value="New York" /> 
     </MergeFields> 
     </Document> 
    </DocCollection> 
    </Batch> 
</Documents> 

所以遠,這是我的蹩腳和可憐的SQL嘗試:

SELECT t.id AS '@BatchID', t.bo_lgl_name AS '@BatchName', 
    (

     SELECT 
      t.document_id AS '@DocumentID', 
      d.description AS '@DocName', 
      ISNULL(t.KeyData, '') AS '@KeyData', 
      (
       SELECT 
        t.document_id AS '@FieldName', 
        d.description AS '@Value' 
       FROM @Data t2 
       INNER JOIN @Documents d2 ON t2.document_id = d1.document_id 
       WHERE t2.id = t1.id AND t2.document_id = t1.document_id 
       FOR XML PATH('MergeField') 
      ) AS 'MergeField' 
     FROM @Data t1 
     INNER JOIN @Documents d1 ON t1.document_id = d.document_id 
     WHERE t1.id = t.id 
     FOR XML PATH('Document'), TYPE 

    ) AS 'DocCollection' 
FROM @Data t 
INNER JOIN @Documents d ON t.document_id = d.document_id 
WHERE value IS NOT NULL 
ORDER BY t.id, t.document_id 
FOR XML PATH('Batch'), ROOT('Documents') 

它讓我部分地在那裏,但是當涉及到在每個標題下對標籤進行分組時,它開始分崩離析。不知道它是否在我的SQL或其他東西中加入。我以前從來沒有嘗試過任何與SQL和XML複雜的事情,所以它可能非常愚蠢。

我在做什麼錯?

感謝

卡爾

回答

2

這應該讓你更接近:

SELECT t.id AS '@BatchID', t.bo_lgl_name AS '@BatchName', 
    (

     SELECT 
      t1.document_id AS '@DocumentID', 
      d.description AS '@DocName', 
      d.document_file as '@FileName', 
      isnull(t1.keydata,'') as '@KeyData', 
      (
       SELECT 
        t2.field_name AS '@FieldName', 
        t2.value AS '@Value' 
       FROM @Data t2 

       WHERE t2.document_id = d.document_id 
       FOR XML PATH('MergeField') , TYPE 
      ) AS 'MergeField' 
     FROM (SELECT DISTINCT document_id,id,keydata FROM @DATA) t1 
     inner join @Documents d 
     on d.document_id=t1.document_id and t.id=t1.id 
     order by d.document_id 
     FOR XML PATH('Document'), TYPE 
    ) AS 'DocCollection' 
FROM (select distinct id,bo_lgl_name from @Data where value is not null) t 
FOR XML PATH('Batch'), ROOT('Documents') 

我不知道你怎麼想建議的輸出。 Taxid'123456789'用文件6標記,但是用文件269列出它?

+0

完美!謝謝。也許我的示例代碼有點偏離,但這正是我所期待的。 – CarlGanz