【发布时间】:2017-08-24 15:51:31
【问题描述】:
有人可以帮我从 Oracle 中的 XMLType 列中检索数据吗?
drop table xml_analysis;
create table xml_analysis(id number,soft_attributes XMLType);
create table xml_softattributes(id number,soft_attributes varchar2(200));
INSERT INTO xml_analysis VALUES
( 1, XMLType(
'<softattributes>
<attr1>ABC</attr1>
<attr2>XYZ</attr2>
<attr3>PQR</attr3>
</softattributes>
'));
insert into xml_softattributes values(1,'attr1');
insert into xml_softattributes values(1,'attr2');
insert into xml_softattributes values(1,'attr3');
- 表 xml_analysis 包含 xmltype 列,我不知道其属性
- 表 xml_softattributes 包含软属性列表(不是 xpath),它们存在于 xml_analysis 表的 xmltype 列中
- 表根据 id 连接
现在我的问题是使用表 xml_softattributes 从 xml_analysis 表中动态检索数据,我该怎么做?
需要输出
Softattribute Value
=======================
attr1 ABC
attr2 XYZ
attr3 PQR
我能想到的可能解决方案是使用动态字符串并执行,但我不希望动态字符串查询来检索数据。
【问题讨论】:
-
属性名称是一成不变的(只能是
'attr1'、'attr2'和'attr3'),还是需要允许它们任意,并根据xml_softattributes?如果是前者,可以用静态代码来完成。否则,您将需要动态代码(很可能),但有人会问为什么您没有更通用的 XML 结构,其中'attr1'和'ABC'、'attr2'和'XYZ'等是相同类型节点的两个“值”。无论哪种方式,这看起来都像是某种 EAV 模型……应该先修复它吗? -
@mathguy:不,属性名称没有模式,它可以是任何东西,基于 xml_softattributes 表值。要求是,我们有一组特定 csv 的软属性,例如在我们的例子中为 1,我们不想在表中创建列,因此查看此 xml_softattributes 表并在 xml_analysis 表中基于该表动态创建 xml。稍后我们需要检索此属性以进行某些报告。但结构将是attribute_name 和 value。我们还可以存储 xml 数据,例如'
'attr1 ABC -
@GauravSoni - 您评论中的最后一句话正是我的建议/建议:如果您的 XML 数据以这种方式构建,您可以使用
XMLTABLE函数从XMLTYPE 事先不知道所有可能属性的名称;并且您不需要 UNPIVOT,数据已经以所需的列格式提取。
标签: sql oracle oracle11g oracle11gr2 xmltype