Updates of entire XML documents
To update an entire XML document in an XML column, use the SQL UPDATE statement. Include a WHERE clause when you want to update specific rows.
The input to the XML column must be a well-formed XML document, as defined in the XML 1.0 specification. A document node will be created implicitly if one does not already exist. The application data type can be XML (XML AS BLOB, XML AS CLOB, XML AS DBCLOB), character, or binary.
XML data in an application can be in textual XML format or extensible dynamic binary XML Db2 client/server binary XML format (binary XML format). Binary XML format is valid only for JDBC, SQLJ, and ODBC applications. When you update data in an XML column, it must be converted to its XML hierarchical format. The Db2 database server performs this operation implicitly when XML data from a host variable directly updates an XML column. Alternatively, you can invoke the XMLPARSE function explicitly when you perform the update operation, to convert the data to the XML hierarchical format.
- Implicitly, if the XML column has an XML type modifier defined on it.
- Explicitly, with the DSN_XMLVALIDATE function.
You can use XML column values to specify which rows are to be updated. To find values within XML documents, you need to use XQuery expressions. One way of specifying XQuery expressions is the XMLEXISTS predicate, which allows you to specify an XQuery expression and determine if the expression results in an empty sequence. When XMLEXISTS is specified in the WHERE clause, rows will be updated if the XQuery expression returns a non-empty sequence.
<customerinfo xmlns="http://posample.org" Cid="1004">
<name>Christine Haas</name>
<addr country="Canada">
<street>12 Topgrove</street>
<city>Toronto</city>
<prov-state>Ontario</prov-state>
<pcode-zip>N9Y-8G9</pcode-zip>
</addr>
<phone type="work">905-555-5238</phone>
<phone type="home">416-555-2934</phone>
</customerinfo>
PreparedStatement updateStmt = null;
String sqls = null;
int cid = 1004;
sqls = "UPDATE Customer SET Info=? WHERE Cid=?";
updateStmt = conn.prepareStatement(sqls);
updateStmt.setInt(2, cid);
File file = new File("c7.xml");
updateStmt.setBinaryStream(1, new FileInputStream(file), (int)file.length());
updateStmt.executeUpdate();EXEC SQL BEGIN DECLARE SECTION;
sqlint64 cid;
SQL TYPE IS XML AS BLOB (10K) xml_hostvar;
EXEC SQL END DECLARE SECTION;
…
cid=1004;
/* Read data from file c7.xml into xml_hostvar */
…
EXEC SQL UPDATE MYCUSTOMER SET xmlcol=:xml_hostvar WHERE Cid=:cid;EXEC SQL UPDATE MYCUSTOMER SET xmlcol=:xml_hostvar
WHERE XMLEXISTS ('declare default element namespace "http://posample.org";
/customerinfo[@Cid = $c]'
passing INFO, cast(:cid as integer) as "c");