SQL 中的 Xquery:选择子集

发布于 2024-10-03 15:39:52 字数 835 浏览 0 评论 0原文

给定以下 xml 变量:

    declare @x xml
    select @x = '<Details>
<Description>
<Attributes>
   <Name>A</Name>
   <Values><RecordId>1</RecordId><RecordId>2</RecordId></Values>
</Attributes>
<Attributes>
    <Name>B</Name>
    <Values><RecordId>3</RecordId><RecordId>4</RecordId></Values>
</Attributes>
</Description>
</Details>'

我试图获取所有 Name 值及其所有 RecordId。我想在一份声明中做到这一点。我现在有以下内容。

create table #xml (element varchar(60))

insert into #xml
select RoleDetails.item.value('(Name)[1]', 'varchar(60)')
from 
  @x.nodes('/Details/Description/Attributes') AS RoleDetails(item)

我正在寻找的格式是:

A 1
  2

B 3
  4

Given the following xml variable:

    declare @x xml
    select @x = '<Details>
<Description>
<Attributes>
   <Name>A</Name>
   <Values><RecordId>1</RecordId><RecordId>2</RecordId></Values>
</Attributes>
<Attributes>
    <Name>B</Name>
    <Values><RecordId>3</RecordId><RecordId>4</RecordId></Values>
</Attributes>
</Description>
</Details>'

I am trying to get all Name values with all their RecordIds. I would like to do it in one statement. I have the following now.

create table #xml (element varchar(60))

insert into #xml
select RoleDetails.item.value('(Name)[1]', 'varchar(60)')
from 
  @x.nodes('/Details/Description/Attributes') AS RoleDetails(item)

The format I'm looking for would be:

A 1
  2

B 3
  4

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

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

发布评论

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

评论(1

跨年 2024-10-10 15:39:52

以 XML 格式保存子集并允许代码执行 XML 操作变得更加容易。

It became easier to save the subset in the XML format and allow for code to do XML manipulation.

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