SQL Server : nesting elements with FOR XML PATH

自闭症网瘾萝莉.ら 提交于 2019-11-30 04:43:48

问题


I want to nest each of my XML elements.

Take the following example:

DECLARE @TempTable TABLE
(
    [Column1] char(10),
    [Column2] char(10)
);

INSERT INTO @TempTable([Column1], [Column2]) VALUES
('some value', 'some value'),
('some value', 'some value'),
('some value', 'some value'),
('some value', 'some value')

SELECT (
    SELECT * FROM @TempTable
FOR XML PATH('Row'), TYPE)
FOR XML PATH('ParentRow'), ROOT('Root')

Which will return the following XML:

  <Root>
    <ParentRow>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
    </ParentRow>
  </Root>

Which is not the way I want the XML to be formatted. Rather I want each <Row> element to be wrapped with a <ParentRow> element like below:

  <Root>
    <ParentRow>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
    </ParentRow>
    <ParentRow>
    <Row>
      <Column1>some value</Column1>
      <Column2>some value</Column2>
    </Row>
    </ParentRow>
    <ParentRow>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
    </ParentRow>
    <ParentRow>
      <Row>
        <Column1>some value</Column1>
        <Column2>some value</Column2>
      </Row>
    </ParentRow>
  </Root>    

Any help with this guys?


回答1:


Okay here is the code.

DECLARE @TempTable TABLE
(
    [Column1] char(10),
    [Column2] char(10)
);

INSERT INTO @TempTable([Column1], [Column2]) VALUES
('some value', 'some value'),
('some value', 'some value'),
('some value', 'some value'),
('some value', 'some value')

SELECT (
        SELECT (
                SELECT T1.*
                FOR XML path('')
                    ,root('Row')
                    ,type
                )
        FROM @TempTable AS T1
        FOR XML path('ParentRow')
            ,type
        )
FOR XML path('Root')

and here is the output.

<Root>
  <ParentRow>
    <Row>
      <Column1>some value</Column1>
      <Column2>some value</Column2>
    </Row>
  </ParentRow>
  <ParentRow>
    <Row>
      <Column1>some value</Column1>
      <Column2>some value</Column2>
    </Row>
  </ParentRow>
  <ParentRow>
    <Row>
      <Column1>some value</Column1>
      <Column2>some value</Column2>
    </Row>
  </ParentRow>
  <ParentRow>
    <Row>
      <Column1>some value</Column1>
      <Column2>some value</Column2>
    </Row>
  </ParentRow>
</Root>

good luck...




回答2:


SELECT (SELECT * FROM @TempTable
        WHERE t.Column1 = Column1
        FOR XML PATH('ParentRow'),ROOT('Row'), TYPE)
FROM @TempTable t
FOR XML PATH(''), ROOT('Root')

Result

<Root>
  <Row>
    <ParentRow>
      <Column1>some value1</Column1>
      <Column2>some value </Column2>
    </ParentRow>
  </Row>
  <Row>
    <ParentRow>
      <Column1>some value2</Column1>
      <Column2>some value </Column2>
    </ParentRow>
  </Row>
  <Row>
    <ParentRow>
      <Column1>some value3</Column1>
      <Column2>some value </Column2>
    </ParentRow>
  </Row>
  <Row>
    <ParentRow>
      <Column1>some value4</Column1>
      <Column2>some value </Column2>
    </ParentRow>
  </Row>
</Root>


来源:https://stackoverflow.com/questions/25770689/sql-server-nesting-elements-with-for-xml-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!