更新我的Oracle数据库行中的XML标记
我想创建一个触发器,用于更新xml文件中的单个节点。我有一个包含两行的表,其中一行是条目的id,另一行是xml类型更新我的Oracle数据库行中的XML标记,xml,oracle,Xml,Oracle,我想创建一个触发器,用于更新xml文件中的单个节点。我有一个包含两行的表,其中一行是条目的id,另一行是xml类型 drop table product_information_xml; create table product_information_xml( information_id Number(6,0), products XMLType); 现在有一个条目是这样的 "INFORMATION_ID" "PRODUCTS" 5 <prod
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>
现在我被卡住了。我很想得到帮助 对,因此触发器中的语句如下所示: 如果产品标识不可为空:
update product_information_xml
set products = updatexml(products, '/products/product_id/text()', :new.product_id)
where information_id = :new.information_id;
如果是,请使用更长的方法,因为updatexml无法从空变为非空:
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;
如果您不知道要更新哪个节点,那么更新整个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;
也许这对你好有帮助。是的,我已经看到了,但问题是,我需要把它放在触发器里。这就是我被困的地方。但是谢谢你的帮助。首先,谢谢你的评论,但问题是如果我需要更新其他内容,我不知道该怎么做。我的意思是我的xml中有10个标记。现在我可以更新产品id了。但是当我想更新产品描述时我该怎么做?我知道我只需要在您的示例中将product_id更改为product_description,但是触发器应该从自身知道要更新哪个节点。Hi。谢谢,这几乎完美无瑕。刚刚将最后一行更改为代码,其中选择p.products.extract'/products/product_id/text'.getStringVal ProductID FROM product_information_xml p=:new.product_id;终止所以现在的问题是,当我多次使用相同的产品id时。我从更新产品信息集列表中得到了一个错误,即我的where子句返回了多行。但是产品id不应该是唯一的。所以这是我唯一要解决的问题
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;