SQL Server : nesting elements with FOR XML PATH

2019-02-13 18:17发布

问题:

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>