Question

I have a Web Service one of whose methods returns a list of items, each of which possesses another list of items:

<TopLevelItems>
    <TopLevelItem field1="a" field2="b" ...>
        <LowLevelItem fieldA="1" fieldB="2" .../>
        <LowLevelItem fieldA="3" fieldB="4" .../>
    </TopLevelItem>
</TopLevelItems>

These lists are retrieved from a SQL Server database using simple queries (both TopLevelItem and LowLevelItem correspond to related tables in the database).

Until now, to retrieve all this data, I needed two queries: one to retrieve top level items, which was executed once; and another to retrieve low level items, which was executed once per top level item.

However, this seems to be highly inefficient. I would like to define a single stored procedure that performs all the necessary queries and retrieves the result as a hierarchical data structure. Is it possible? If so, how?

Was it helpful?

Solution

Hierarchical data in SQL server can be obtained using FOR XML. In this case, you would just need to write a query to join tables, then parent-child relationships will manifest as nested XML elements:

DECLARE @sites TABLE ( ID INT, Name VARCHAR(50) )
INSERT  INTO @sites
VALUES  ( 1, 'abc' ),
        ( 2, 'def' )

DECLARE @siteEnergy TABLE
  (
    SiteFK INT,
    Month INT,
    Energy INT
  )
INSERT  INTO @siteEnergy
VALUES  ( 1, 1, 50 ),
        ( 1, 2, 49 ),
        ( 1, 3, 50 ),
        ( 2, 1, 33 ),
        ( 2, 2, 34 ),
        ( 2, 3, 50 )

SELECT  *
FROM    @sites site
        JOIN @siteEnergy siteEnergy ON site.id = siteEnergy.sitefk
FOR     XML AUTO, ROOT('SiteInformation')

Results:

<SiteInformation>
  <site ID="1" Name="abc">
    <siteEnergy SiteFK="1" Month="1" Energy="50" />
    <siteEnergy SiteFK="1" Month="2" Energy="49" />
    <siteEnergy SiteFK="1" Month="3" Energy="50" />
  </site>
  <site ID="2" Name="def">
    <siteEnergy SiteFK="2" Month="1" Energy="33" />
    <siteEnergy SiteFK="2" Month="2" Energy="34" />
    <siteEnergy SiteFK="2" Month="3" Energy="50" />
  </site>
</SiteInformation>

OTHER TIPS

Use can create the XML in the SP directly

Example

declare @TopLevelItem table (TopID int, field1 varchar(50), field2 varchar(50))
declare @LowLevelItem table (TopID int, fieldA int, fieldB int)

insert into @TopLevelItem values (1, 'a', 'b')
insert into @LowLevelItem values (1, 1, 2)
insert into @LowLevelItem values (1, 3, 4)

select 
  T.field1 as '@field1',
  T.field2 as '@field2',
  ((select 
      L.fieldA as '@fieldA',
      L.fieldB as '@fieldB'
    from @LowLevelItem as L
    where T.TopID = L.TopID
    for xml path('LowLevelItem'), type))
from @TopLevelItem as T
for xml path('TopLevelItem'), root('TopLevelItems') 

Result

<TopLevelItems>
  <TopLevelItem field1="a" field2="b">
    <LowLevelItem fieldA="1" fieldB="2" />
    <LowLevelItem fieldA="3" fieldB="4" />
  </TopLevelItem>
</TopLevelItems>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top