sql server 2000 和 xml 显式

发布于 2024-08-23 09:23:33 字数 560 浏览 3 评论 0原文

我在 SQL Server 2000 中显式使用 for xml 时遇到问题(因此我无法使用 sql 2005/8 中的新 path() 内容)

本质上我有两个表,我想要的 XML 结构

<xml>
  <table_1 field1="foo" field2="foobar2" field3="foobar3">
      <a_row_from_table_2 field1="goo" field2="goobar2" field3="goobar3" />
      <a_row_from_table_2 field1="hoo" field2="hoobar2" field3="hoobar3" />
  </table_1>
</xml>

是,table_1与table_2是一对多的关系,我想将它建立一个层次结构。

到目前为止我似乎无法得到它,我设法得到的最接近的是表1中的所有记录,表2中的所有记录附加到表1的最后一个元素

任何有关设置这种关系的帮助都会不胜感激。

-马尔辛

I've got a problem with using for xml explicit in SQL Server 2000 (so I can't use the new path() stuff from sql 2005/8)

Essentially I have two tables and the XML structure I want to have is

<xml>
  <table_1 field1="foo" field2="foobar2" field3="foobar3">
      <a_row_from_table_2 field1="goo" field2="goobar2" field3="goobar3" />
      <a_row_from_table_2 field1="hoo" field2="hoobar2" field3="hoobar3" />
  </table_1>
</xml>

That is, table_1 has a one-to-many relationship with table_2, and I want to make a hierarchy of it.

So far I can't seem to get it, the closest I've managed to get is all the records from table1, with all the records from table2 appended to the very last element of table1

Any help with setting up this kind of relationship would be greatly appreciated.

-Marcin

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

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

发布评论

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

评论(1

紫南 2024-08-30 09:23:34

您的查询是否包含 order by 子句?在 for xml 显式 模式中,信息按所需顺序出现非常重要。例如,在 Northwind 示例数据库中,您可以为员工区域生成嵌套 XML,如下所示:

select
    1 as Tag,
    Null as Parent,
    e.EmployeeId as [Employee!1!EmployeeId],
    e.LastName as [Employee!1!LastName],
    Null as [EmployeeTerritories!2!TerritoryID]
from dbo.Employees e
union all
select 2 as Tag,
    1 as Parent,
    et.EmployeeId as [Employee!1!EmployeeId],
    Null as [Employee!1!LastName],
    et.TerritoryId as [EmployeeTerritories!2!TerritoryID]
from dbo.EmployeeTerritories et
order by [Employee!1!EmployeeId], Parent
for xml explicit

这会生成如下 XML:

<Employee EmployeeId="1" LastName="Davolio">
  <EmployeeTerritories TerritoryID="06897" />
  <EmployeeTerritories TerritoryID="19713" />
</Employee>
<Employee EmployeeId="2" LastName="Fuller">
...

要了解其工作原理,请在不使用 for xml 显式的情况下运行查询

Does your query include an order by clause? In for xml explicit mode, it's important that information appears in the sequence it's needed. For example, in the Northwind example database, you can generate a nested XML for employee territories like:

select
    1 as Tag,
    Null as Parent,
    e.EmployeeId as [Employee!1!EmployeeId],
    e.LastName as [Employee!1!LastName],
    Null as [EmployeeTerritories!2!TerritoryID]
from dbo.Employees e
union all
select 2 as Tag,
    1 as Parent,
    et.EmployeeId as [Employee!1!EmployeeId],
    Null as [Employee!1!LastName],
    et.TerritoryId as [EmployeeTerritories!2!TerritoryID]
from dbo.EmployeeTerritories et
order by [Employee!1!EmployeeId], Parent
for xml explicit

This results in an XML like:

<Employee EmployeeId="1" LastName="Davolio">
  <EmployeeTerritories TerritoryID="06897" />
  <EmployeeTerritories TerritoryID="19713" />
</Employee>
<Employee EmployeeId="2" LastName="Fuller">
...

To get an idea of how it works, run the query without the for xml explicit.

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