技术开发 频道

将XML应用程序从DB2 8.x迁移到Viper

  XMLUpdate 示例

  对于 XMLUpdate 示例,请执行以下步骤:

  创建测试表:

Create table XMLCustomer(cid integer not null PRIMARY KEY, info XML );

  将示例 XML 文档插入表中:

Insert into XMLCustomer (cid, info ) values (1006 , XMLPARSE ( DOCUMENT ' <customerinfo xmlns=" http://posample.org " Cid="1006"> <name>Hardeep Singh</name> <addr country="United States"> <street>555 Bailey Ave</street> <city/> <prov-state>CA</prov-state> <pcode-zip> 95141</pcode-zip> </addr> <phone type="">543-4610</phone> </customerinfo>' PRESERVE WHITESPACE ) ); 

  注意:由于更新调用修改了初始的 XML 文档,所以您需要为某些查询而删除插入的文档,并重新插入它。

  示例查询

  下面是示例查询:

  替换节点:action=replace。

  通过使用复杂名称元素替换简单名称元素来更新测试文档:

Call DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update action="replace" col="1" path="/x:customerinfo/x:name"> <name><fname>Hardeep</fname><lname>Singh</lname></name> </update> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=? where cid=1006',?,?);

  使用 SQL 查询获取新值以进行更新:

using=SQL。 Call DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update using="sql" action="replace" col="1" path="//x:customerinfo[@Cid=1006]/x:addr/x:pcode-zip/text()"> select cid from XMLCustomer where cid=1006 </update> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=? where cid=1006',?,?);

  使用给定表达式来计算值:

action=computeCall DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update action="compute" col="1" path="/x:customerinfo/x:addr/x:pcode-zip/text()"> (20+?)*32-? </update> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=? &yuml;here cid=1006',?,?);

  对目标 XML 文档执行多个操作:

Call DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update using="sql" action="replace" col="1" path="/x:customerinfo/x:addr/x:pcode-zip/text()"> select cid from XMLCustomer where cid=1006 </update> <update action="compute" col="1" path="/x:customerinfo/x:addr/x:pcode-zip/text()"> (2+?)*10-? </update> <update action="delete" col="1" path="/x:customerinfo/x:name"/> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=? where cid=1006',?,?);

  更新文档时对其进行验证。

  为此,您需要创建模式并在 XSR 中注册。

Call DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update using="sql" action="replace" col="1" path="/x:customerinfo/x:addr/x:pcode-zip/text()"> select cid from XMLCustomer where cid=1006 </update> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=xmlvalidate( ? according to XMLSCHEMA ID test.schema2) where cid=1006',?,?)

  使用 XMLUpdate 替换属性值。

Call DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update action="replace" col="1" path="/x:customerinfo/x:phone/@type"> tie line </update> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=? where cid=1006',?,?);

  使用 XMLUpdate 替换文本值。

Call DB2XMLFUNCTIONS.XMLUPDATE ( '<updates namespaces="x:http://posample.org"> <update action="replace" col="1" path="/x:customerinfo/x:addr/x:city/text()"> San Jose </update> </updates>', 'Select info from XMLCustomer where cid=1006', 'update XMLCustomer set info=? where cid=1006',?,?);
0
相关文章