2017-08-04 15 views
1

を考えると、これらのテーブルを取得します。SQL Serverからの複雑なXML 2016

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> 

を、このデータが取り込ま

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) 
) 

これは私の恥ずべき哀れな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やその他のものにJOINがあるかどうかはわかりません。私はこれほど複雑なことを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

関連する問題