我可以从 SQL Server 存储过程中检索分层数据结构吗?

发布于 2024-10-23 11:57:25 字数 565 浏览 6 评论 0原文

我有一个 Web 服务,其中一个方法返回一个项目列表,每个项目都拥有另一个项目列表:

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

这些列表是使用简单查询(TopLevelItemLowLevelItem对应数据库中的相关表)。

到目前为止,要检索所有这些数据,我需要两个查询:一个用于检索顶级项目,该查询执行一次;另一个用于检索顶级项目,该查询仅执行一次。另一个用于检索低级别项目,每个顶级项目执行一次。

然而,这似乎效率极低。我想定义一个存储过程,它执行所有必要的查询并以分层数据结构的形式检索结果。是否可以?如果是这样,怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-10-30 11:57:25

SQL Server中的层次结构数据可以使用FOR XML来获取。在这种情况下,您只需要编写一个查询来连接表,然后父子关系将表现为嵌套的 XML 元素:

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')

结果:

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

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>
望笑 2024-10-30 11:57:25

使用可以直接在 SP 中创建 XML

示例

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') 

结果

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文