更新Oracle数据库行中的XML标记

时间:2012-11-11 11:16:33

标签: xml oracle

我想创建一个触发器,它会更新xml文件中的单个节点。我有一个包含2行的表,其中一个是条目的id,另一个是xml类型

drop table product_information_xml;
create table product_information_xml(
information_id Number(6,0),
products XMLType);

所以,现在一个条目看起来像这样

"INFORMATION_ID"    "PRODUCTS"
5                  <products>
                       <product_id>1</product_id>
                       <product_name>samsung galaxy note</product_name>
                       <product_description>mobile phone</product_description>
                       <category_id>11</category_id>
                       <weight_class>2</weight_class>
                       <warranty_period>+000000000-06</warranty_period>
                       <supplier_id>102069</supplier_id>
                       <product_status>orderable</product_status>
                  <list_price>500</list_price>
                  <min_price>400</min_price>
                  <catalog_url>www.samsung.de</catalog_url>
              </products>

所以现在我有另一张桌子不是xml。并将所有XML标记作为单个列。因此列为product_idproduct_description等。

当我更新product_id时如何更新xml表中的xml节点<product_id>?有人可以帮忙吗?

我所知道的是我从

开始
Create or replace trigger delete_xml
after update on product_information
for each row
begin
update ?????
end;

现在我被卡住了。我很乐意帮忙!

2 个答案:

答案 0 :(得分:0)

正确,因此触发器中的语句将如下所示:

如果product_id不可为空:

update product_information_xml
     set products = updatexml(products, '/products/product_id/text()', :new.product_id)
   where information_id = :new.information_id;

如果是,请使用更长的方法(因为updatexml不能从NULL转为非NULL:

update product_information_xml
     set products = insertxmlbefore(deletexml(products, '/products/product_id'), 
                         '/products/product_name', 
                        xmltype('<product_id>' || :new.product_id|| '</product_id>'))
   where information_id = :new.information_id;

答案 1 :(得分:0)

如果您不知道要更新哪个节点,可能更容易更新整个XMLTYPE -

update product_information_xml
   set products = xmltype('<products><product_id>'||:new.product_id||'</product_id>'||
                   '<product_name>'|| :new.product_name||'</product_name>'||
                   '<product_description>'|| :new.product_description||'</product_description>'||
                   '<category_id>'|| :new.category_id||'</category_id>'||
                   '<weight_class>'|| :new.weight_class||'</weight_class>'||
                   '<warranty_period>'|| :new.warranty_period||'</warranty_period>'||
                   '<supplier_id>'|| :new.supplier_id||'</supplier_id>'||
                   '<product_status>'|| :new.product_status||'</product_status>'||
                   '<list_price>'|| :new.list_price||'</list_price>'||
                   '<min_price>'|| :new.min_price||'</min_price>'||
                   '<catalog_url>'|| :new.catalog_url||'</catalog_url></products>')
 where information_id = :new.information_id;
相关问题