| | |
XML Update??
Please support our RSS, Web Services and SOAP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Solved Threads: 0
DB2 9 does not support updating an element in XML documents, you must perform a full-document update using the UPDATE SQL statement.
UPDATE customer SET info =
'<customerinfo xmlns="http://posample.org" Cid="1002">
<name>Jim Noodle</name>
<addr country="Canada">
<street>1150 Maple Drive</street>
<city>Newtown</city>
<prov-state>Ontario</prov-state>
<pcode-zip>Z9Z 2P2</pcode-zip>
</addr>
<phone type="work">905-555-7258</phone>
</customerinfo>'
WHERE XMLEXISTS (
'declare default element namespace "http://posample.org";
$doc/customerinfo[@Cid = 1002]'
passing INFO as "doc")
The XMLEXISTS predicate ensures that only the document containing the attribute Cid="1002" is replaced. Notice how the predicate expression in XMLEXISTS, [@Cid = 1002], is not specified as a string comparison: [@Cid = "1002"]. This is because the index, previously created, for the Cid attribute was defined with the DOUBLE data type. In order for the index to match this query, Cid cannot be specified as a string in the predicate expression.
MTK can be downloaded at
http://www-306.ibm.com/software/data/db2/migration/mtk/. More information can be found at
http://www-1.ibm.com/support/docview...id=swg27009230.
UPDATE customer SET info =
'<customerinfo xmlns="http://posample.org" Cid="1002">
<name>Jim Noodle</name>
<addr country="Canada">
<street>1150 Maple Drive</street>
<city>Newtown</city>
<prov-state>Ontario</prov-state>
<pcode-zip>Z9Z 2P2</pcode-zip>
</addr>
<phone type="work">905-555-7258</phone>
</customerinfo>'
WHERE XMLEXISTS (
'declare default element namespace "http://posample.org";
$doc/customerinfo[@Cid = 1002]'
passing INFO as "doc")
The XMLEXISTS predicate ensures that only the document containing the attribute Cid="1002" is replaced. Notice how the predicate expression in XMLEXISTS, [@Cid = 1002], is not specified as a string comparison: [@Cid = "1002"]. This is because the index, previously created, for the Cid attribute was defined with the DOUBLE data type. In order for the index to match this query, Cid cannot be specified as a string in the predicate expression.
MTK can be downloaded at
http://www-306.ibm.com/software/data/db2/migration/mtk/. More information can be found at
http://www-1.ibm.com/support/docview...id=swg27009230.
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Solved Threads: 0
XML documents are stored natively in DB2 and there is no out-of-the-box functionality for performing sub-document updates.
One solution to this problem is to bring the document over the client, modify it, and then save it back into the database. This approach is limited by the XML capabilities of the client environment.
There is another solution. By creating an update stored procedure it is possible to update XML documents in the database without having to bring them over the client. This stored procedure enables partial updates of XML documents.
For example, DB2XMLFUNCTIONS.XMLUPDATE is a java-based stored procedure. The following example updates XML document by replacing the simple name element with a complex name element.
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 CUSTOMER where cid=1006',
'update CUSTOMER set info=? where cid=1006',?,?);
You can download jar file and examples at:
http://www.ibm.com/developerworks/db.../dm-0605singh/
One solution to this problem is to bring the document over the client, modify it, and then save it back into the database. This approach is limited by the XML capabilities of the client environment.
There is another solution. By creating an update stored procedure it is possible to update XML documents in the database without having to bring them over the client. This stored procedure enables partial updates of XML documents.
For example, DB2XMLFUNCTIONS.XMLUPDATE is a java-based stored procedure. The following example updates XML document by replacing the simple name element with a complex name element.
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 CUSTOMER where cid=1006',
'update CUSTOMER set info=? where cid=1006',?,?);
You can download jar file and examples at:
http://www.ibm.com/developerworks/db.../dm-0605singh/
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Solved Threads: 0
Steps to setup the update stored procedure:
i) Download db2xmlfunctions.jar file and source code from http://www.ibm.com/developerworks/vi...cale=worldwide
ii) On Microsoft Windows, open a DB2 command window:
SET CLASSPATH= .;%DB2PATH%\java\db2java.zip;
%DB2PATH%\java\db2jcc.jar;
%DB2PATH%\java\db2jcc_license_cisuz.jar;
"%DB2PATH%\java\jdk\bin\javac.exe" -d . *.java
"%DB2PATH%\java\jdk\bin\jar" cvf db2xmlfunctions.jar com/ibm/db2/xml/functions/*.class
iii) Install the stored procedure in DB2
DB2 -t
connect to your_dbname;
CALL SQLJ.INSTALL_JAR('file:/temp/samples/db2xmlfunctions.jar' ,
db2xmlfunctions,0);
iv) Register the stored procedure in your database:
CREATE PROCEDURE db2xmlfunctions.XMLUPDATE(
IN COMMANDSQL VARCHAR(32000),
IN QUERYSQL VARCHAR(32000),
IN UPDATESQL VARCHAR(32000),
OUT errorCode INTEGER, OUT errorMsg VARCHAR(32000))
DYNAMIC RESULT SETS 0
LANGUAGE JAVA
PARAMETER STYLE JAVA
NO DBINFO
FENCED
NULL CALL MODIFIES SQL DATA
PROGRAM TYPE SUB
EXTERNAL NAME b2xmlfunctions:com.ibm.db2.xml.functions.XMLUpdate.Update';
TERMINATE;
You can read more at:
http://www.ibm.com/developerworks/db.../dm-0605singh/
i) Download db2xmlfunctions.jar file and source code from http://www.ibm.com/developerworks/vi...cale=worldwide
ii) On Microsoft Windows, open a DB2 command window:
SET CLASSPATH= .;%DB2PATH%\java\db2java.zip;
%DB2PATH%\java\db2jcc.jar;
%DB2PATH%\java\db2jcc_license_cisuz.jar;
"%DB2PATH%\java\jdk\bin\javac.exe" -d . *.java
"%DB2PATH%\java\jdk\bin\jar" cvf db2xmlfunctions.jar com/ibm/db2/xml/functions/*.class
iii) Install the stored procedure in DB2
DB2 -t
connect to your_dbname;
CALL SQLJ.INSTALL_JAR('file:/temp/samples/db2xmlfunctions.jar' ,
db2xmlfunctions,0);
iv) Register the stored procedure in your database:
CREATE PROCEDURE db2xmlfunctions.XMLUPDATE(
IN COMMANDSQL VARCHAR(32000),
IN QUERYSQL VARCHAR(32000),
IN UPDATESQL VARCHAR(32000),
OUT errorCode INTEGER, OUT errorMsg VARCHAR(32000))
DYNAMIC RESULT SETS 0
LANGUAGE JAVA
PARAMETER STYLE JAVA
NO DBINFO
FENCED
NULL CALL MODIFIES SQL DATA
PROGRAM TYPE SUB
EXTERNAL NAME b2xmlfunctions:com.ibm.db2.xml.functions.XMLUpdate.Update';
TERMINATE;
You can read more at:
http://www.ibm.com/developerworks/db.../dm-0605singh/
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Solved Threads: 0
The java based xml update stored procedure can be used for:
i) Changing the value of any text or attribute nodes in the target XML document.
ii) Replacing an element node in the XML document with another XML element along with all it child nodes.
iii) Deleing a node in the XML document.
iv) Inserting a new element.
v) Multiple updates to the source documents.
vi) Updates to multiple source documents.
vii) Replacing another XML document with the modified one.
viii) Inserting the modified document into a new record.
The update information can be:
i) Statically embedded in the update call
ii) Dynamically created at runtime using SQL.
iii) Computed using an arithmetic expression on the original text or attribute value.
The syntax of XMLUPDTE function is:
DB2XMLFUNCTIONS.XMLUPDATE (commandXML, querySQL, updateSQL, errorCode, errorMsg)
· commandXML à this argument is an XML string that encapsulates the update commands. These commands are then applied to the XML documents selected by the querySQL.
The structure of this command is:
<updates namespaces="">
<update using="" col="" action="" path="">update value</update>
</updates>
· querySQL à any valid SQL select statement that retrieves the XML documents that need to be update.
· updateXQL à It represents a parameterized update SQL. The modified XML document is bound to the update SQL as a runtime parameter. It allows a modified XML document to be saved to other XML columns in the database.
· errorCode à a value of -1 indicates that the stored procedure was aborted due to some error. If the update was successful, a positive value indicating the number of records that have been updated is returned.
· errorMsg à error message, including any exception thrown by the XML parse and the JCC driver.
You can find more information at:
http://www.ibm.com/developerworks/db.../dm-0605singh/
i) Changing the value of any text or attribute nodes in the target XML document.
ii) Replacing an element node in the XML document with another XML element along with all it child nodes.
iii) Deleing a node in the XML document.
iv) Inserting a new element.
v) Multiple updates to the source documents.
vi) Updates to multiple source documents.
vii) Replacing another XML document with the modified one.
viii) Inserting the modified document into a new record.
The update information can be:
i) Statically embedded in the update call
ii) Dynamically created at runtime using SQL.
iii) Computed using an arithmetic expression on the original text or attribute value.
The syntax of XMLUPDTE function is:
DB2XMLFUNCTIONS.XMLUPDATE (commandXML, querySQL, updateSQL, errorCode, errorMsg)
· commandXML à this argument is an XML string that encapsulates the update commands. These commands are then applied to the XML documents selected by the querySQL.
The structure of this command is:
<updates namespaces="">
<update using="" col="" action="" path="">update value</update>
</updates>
· querySQL à any valid SQL select statement that retrieves the XML documents that need to be update.
· updateXQL à It represents a parameterized update SQL. The modified XML document is bound to the update SQL as a runtime parameter. It allows a modified XML document to be saved to other XML columns in the database.
· errorCode à a value of -1 indicates that the stored procedure was aborted due to some error. If the update was successful, a positive value indicating the number of records that have been updated is returned.
· errorMsg à error message, including any exception thrown by the XML parse and the JCC driver.
You can find more information at:
http://www.ibm.com/developerworks/db.../dm-0605singh/
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Solved Threads: 0
If you make any changes to the stored procedure, you should first drop the store procedure from DB2 and then call remove stored procedure jar statement. You can use below steps to remove stored procedure.
//dropping db2 xml update stored procedure.
DROP PROCEDURE DB2XMLFUNCTIONS.XMLUPDATE(VARCHAR(32000),
VARCHAR(32000),VARCHAR(32000),INTEGER, VARCHAR(32000));
//removing xml update java based stored procedure jar.
CALL SQLJ.REMOVE_JAR(DB2XMLFUNCTIONS);
//dropping db2 xml update stored procedure.
DROP PROCEDURE DB2XMLFUNCTIONS.XMLUPDATE(VARCHAR(32000),
VARCHAR(32000),VARCHAR(32000),INTEGER, VARCHAR(32000));
//removing xml update java based stored procedure jar.
CALL SQLJ.REMOVE_JAR(DB2XMLFUNCTIONS);
![]() |
Similar Threads
- Solution for Microsoft Update blank page and other things (Web Browsers)
- Searching and Comparing strings from an XML Document (Python)
- Starware removal (Viruses, Spyware and other Nasties)
- compare 2 xml files with csharp (C#)
- UPDATE question for ms sql 2000 (MS SQL)
- Dynamically Update Your Web Pages Via E-Mail (Search Engine Optimization)
- working with xml files without physically storing them. (ASP.NET)
- html vs. XML (JavaScript / DHTML / AJAX)
- windows update not work!! (Windows 95 / 98 / Me)
Other Threads in the RSS, Web Services and SOAP Forum
- Previous Thread: XML Management
- Next Thread: COntinous category List
| Thread Tools | Search this Thread |
.htaccess 301 accept access alltop api authentication binarysecuritytoken blog card collaboration credit data development ebay email evernote flash google government highrise htaccess intel internet legal live patent paypal php podcast proxy redirect rss rssfeeds searchmonkey server service soap software swappingxmlfromflash swappingxmlnodes url web webservices webservicesecurity wiki wikipedia xml xslt y!os yahoo ydn





