Search code examples
t-sqlfor-xml

Nesting of FOR XML Expressions


I tried a FOR XML query similar to what's shown here under the subtitle 'Nesting of FOR XML Expressions'. My problem is that I have to have three XML element levels. The results should be something like this.

<StepTree Name="ALFKI">
  <Step Name="Foo">
    <Result id="123" />
    <Result id="456" />
  </Step>
  <Step Name="Bar">
    <Result id="789" />
    <Result id="987" />
  </Step>
</StepTree >

I tried this type of query.

SELECT 1 as TAG,
       NULL as Parent,
       StepTrees.Name AS [StepTree!1!Name],
       NULL as [Step!2!Name],
       NULL as [Result!3!id]
FROM StepTrees
WHERE StepTrees.Name = 'ALFKI' 
UNION ALL
SELECT 2,
       1,
       StepTrees.Name,
       Steps.Name,
       NULL
FROM Steps
JOIN StepTrees ON Steps.StepTreeId = StepTrees.Id
WHERE StepTrees.Name = 'ALFKI' 
UNION ALL
SELECT DISTINCT 3,
       2,
       StepTrees.Name,
       Steps.Name,
       Results.id
FROM StepTrees
JOIN Steps ON Steps.StepTreeId = StepTrees.Id
JOIN Results ON Steps.StepId = Results.StepId
FOR XML EXPLICIT

The resulting XML is as follows.

<StepTree Name="ALFKI">
  <Step Name="Foo" />
  <Step Name="Bar">
    <Result id="123" />
    <Result id="456" />
    <Result id="789" />
    <Result id="987" />
  </Step>
</StepTree >

Any ideas?


Solution

  • Use this query:

    SELECT 1 as TAG,
       NULL as Parent,
       StepTrees.Name AS [StepTree!1!Name],
       NULL as [Step!2!Name],
       NULL as [Result!3!id]
    FROM StepTrees
    WHERE StepTrees.Name = 'ALFKI' 
    UNION ALL
    SELECT 2,
           1,
           Null,
           Steps.Name,
           NULL
    FROM Steps
    JOIN StepTrees ON Steps.StepTreeId = StepTrees.Id
    WHERE StepTrees.Name = 'ALFKI' 
    UNION ALL
    SELECT DISTINCT 3,
           2,
           Null,
           Steps.Name,
           Results.id
    FROM StepTrees
    JOIN Steps ON Steps.StepTreeId = StepTrees.Id
    JOIN Results ON Steps.StepId = Results.StepId
    ORDER BY [Step!2!Name],[Result!3!id]
    FOR XML EXPLICIT