SQL Server : nesting elements with FOR XML PATH
Asked Answered
L

2

6

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?

Lithiasis answered 10/9, 2014 at 16:34 Comment(0)
T
6

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

Trudge answered 10/9, 2014 at 17:9 Comment(0)
C
0
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>
Calvano answered 10/9, 2014 at 17:12 Comment(1)
When I ran that query I did not get the result you pasted above.Lithiasis

© 2022 - 2024 — McMap. All rights reserved.