FOR XML multiple control by attribute in tree concept(FOR XML 通过树概念中的属性进行多重控制)
问题描述
我想弄清楚一个问题.
我已经对简单的订购问题有疑问,但我想订购更多详细信息.在此链接下方检查:SQL Server : FOR XML 按属性排序控制
I already had question about simple ordering issue but I want to order more detail. check below this link : SQL Server : FOR XML sorting control by attribute
我做了一个例子.
SQL 查询.
select (
select '123' AS '@id', (
select
(
select 'test' AS '@testid' , '20' AS '@order'
FOR XML path ('tree') , TYPE
),
(
select 'test2' AS '@testid' , '30' AS '@order'
FOR XML path ('tree-order') , TYPE
),
(
select 'test' AS '@testid' , '10' AS '@order'
FOR XML path ('tree') , TYPE
)
FOR XML path ('Node') , TYPE
)
FOR XML path ('Sample') , TYPE
),
(select '456' AS '@id', (
select
(
select 'test' AS '@testid' , '20' AS '@order'
FOR XML path ('tree') , TYPE
),
(
select 'test2' AS '@testid' , '30' AS '@order'
FOR XML path ('tree-order') , TYPE
),
(
select 'test' AS '@testid' , '10' AS '@order'
FOR XML path ('tree') , TYPE
)
FOR XML path ('Node') , TYPE
)
FOR XML path ('Sample') , TYPE)
FOR XML path ('Main') , TYPE
结果:
<Main>
<Sample id="123">
<Node>
<tree testid="test" order="20" />
<tree-order testid="test2" order="30" />
<tree testid="test" order="10" />
</Node>
</Sample>
<Sample id="456">
<Node>
<tree testid="test" order="20" />
<tree-order testid="test2" order="30" />
<tree testid="test" order="10" />
</Node>
</Sample>
</Main>
预期结果:
<Main>
<Sample id="123">
<Node>
<tree testid="test" order="10" />
<tree testid="test" order="20" />
<tree-order testid="test2" order="30" />
</Node>
</Sample>
<Sample id="456">
<Node>
<tree testid="test" order="10" />
<tree testid="test" order="20" />
<tree-order testid="test2" order="30" />
</Node>
</Sample>
</Main>
最终结果:
<Main>
<Sample id="123">
<Node>
<tree testid="test" />
<tree testid="test" />
<tree-order testid="test2" />
</Node>
</Sample>
<Sample id="456">
<Node>
<tree testid="test" />
<tree testid="test" />
<tree-order testid="test2" />
</Node>
</Sample>
</Main>
这是按树序排列的.
最后我不想在属性中显示订单信息
finally I don't want to show order information in attribute
有人有好主意吗?
感谢所有对此感兴趣的人.
Thank you for everybody who interesting to this.
更新----------------------------------------
Updated ----------------------------------------
谢谢大家,最后我解决了以下关于 order by 和 remove 属性问题的问题:
Thank you every body finally I solved problem as below about order by and remove attribute issue :
declare @resultData xml = (select @data.query('
element Main {
for $s in Main/Sample
return element Sample {
$s/@*,
for $n in $s/Node
return element Node {
for $i in $n/*
order by $i/@order
return $i
}
}
}'));
SET @resultData.modify('delete (Main/Sample/Node/tree/@order)');
SET @resultData.modify('delete (Main/Sample/Node/tree-order/@order)');
select @resultData
推荐答案
select @data.query('
element Main {
for $s in Main/Sample
return element Sample {
$s/@*,
for $n in $s/Node
return element Node {
for $i in Node/*
order by $i/@order
return
if ($i/self::tree)
then element tree { $i/@testid }
else element tree-order { $i/@testid }
}
}
}
}')
这篇关于FOR XML 通过树概念中的属性进行多重控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FOR XML 通过树概念中的属性进行多重控制
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01