简体   繁体   English

从SQL Server 2016检索复杂的XML

[英]Retrieve complex XML from SQL Server 2016

Given these tables: 给定这些表:

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

populated with this data: 填充此数据:

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)

I'm trying to retrieve this XML: 我正在尝试检索此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>

So far, this is my lame and pathetic SQL attempt: 到目前为止,这是我la脚而可悲的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')

which gets me partly there but starts to fall apart when it comes to grouping tags under each heading. 这让我部分到达那里,但在将标签分组到每个标题下时开始崩溃。 Not sure if its the JOINs in my SQL or something else. 不知道它是否在我的SQL或其他中的JOIN。 I've never tried anything this complex with SQL and XML before so it could very well be something silly. 在此之前,我从未尝试过使用SQL和XML进行如此复杂的操作,因此这很愚蠢。

What am I doing wrong? 我究竟做错了什么?

Thanks 谢谢

Carl 卡尔

This should get you closer: 这应该使您更接近:

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

I am not sure how you want to get the suggested output. 我不确定您要如何获得建议的输出。 Taxid '123456789' is marked with document 6, but you list it with document 269? 计程车“ 123456789”标记有文档6,但您用文档269列出了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM