2017-08-04 191 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