我有一个带有xmltype列的Oracle表,该列以下列格式存储XML
<?xml version="1.0" encoding="WINDOWS-1252"?>
<View>
<ReportValues>
<SalaryValue variable="HR" value="999"/>
<SalaryValue variable="floor" value="20"/>
</ReportValues>
</View>
我想知道如何将值从999更新为666变量&#34; HR&#34;还有来自&#34; floor&#34;的变量值。到&#34;销售&#34;
答案 0 :(得分:1)
虽然@АнатолийПредеин的回答绝对正确10g和11g,但需要注意updatexml
已deprecated in Oracle 12c。
从12cR1开始,推荐的操作XML的方法是XQuery Update Facility。它不是特定于Oracle,而是W3C建议书也实现了许多其他XML工具。
下面你会找到一个完整的例子。但是,我不会详细介绍XQuery,而是指向以下文档:
示例设置
create table so61_t(
id number
,xml xmltype
);
insert into so61_t values(1, '<?xml version="1.0" encoding="WINDOWS-1252"?>
<View>
<ReportValues>
<SalaryValue variable="HR" value="999"/>
<SalaryValue variable="floor" value="20"/>
</ReportValues>
</View>');
insert into so61_t values(2, '<?xml version="1.0" encoding="WINDOWS-1252"?>
<View>
<ReportValues>
<SalaryValue variable="HR" value="998"/>
<SalaryValue variable="floor" value="19"/>
</ReportValues>
</View>');
修改XML
update so61_t set xml =
xmlquery(
'copy $t := $x modify(
(for $i in $t/View/ReportValues/SalaryValue[@variable="HR"]/@value
return replace value of node $i with ''666'')
,(for $i in $t/View/ReportValues/SalaryValue[@variable="floor"]/@value
return replace value of node $i with ''SALES'')
) return $t'
passing xml as "x" returning content
)
where id = 1
;
<强>结果
SQL> col id for 99
SQL> col xml for a78
SQL> select id, xmlserialize(content xml as varchar2(200)) as xml from so61_t;
ID XML
--- -------------------------------------------------
1 <?xml version="1.0" encoding="UTF-8"?>
<View>
<ReportValues>
<SalaryValue variable="HR" value="666"/>
<SalaryValue variable="floor" value="SALES"/>
</ReportValues>
</View>
2 <?xml version="1.0" encoding="UTF-8"?>
<View>
<ReportValues>
<SalaryValue variable="HR" value="998"/>
<SalaryValue variable="floor" value="19"/>
</ReportValues>
</View>
SQL>
答案 1 :(得分:0)
检查UPDATEXML orace函数(https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions205.htm)
<?xml version="1.0" encoding="WINDOWS-1252"?>
<View>
<ReportValues>
<SalaryValue variable="HR" value="666"/>
<SalaryValue variable="floor" value="20"/>
</ReportValues>
</View>
返回:
select updatexml( x,'/View/ReportValues/SalaryValue[@variable="floor"]/@variable','SALES' )
from (
select updatexml( xmltype.createxml(xml.xml),'/View/ReportValues/SalaryValue[@variable="HR"]/@value','666' ) x
from xml )
for&#34; floor&#34;变量看起来像这样:
{{1}}