16.10.2015 Views

Getting Started with DB2 Express-C

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15 - <strong>DB2</strong> pureXML 283<br />

15.3.8 Update and delete operations<br />

Update and delete operations on XML data can be performed in one of two ways:<br />

• Using SQL UPDATE and DELETE statements<br />

• Using the TRANSFORM expression<br />

For the first way using SQL UPDATE and DELETE statements, the update or delete occurs<br />

at the document level; that is, the entire XML document is replaced <strong>with</strong> the updated one.<br />

For example, in the UPDATE statement in Listing 15.25 below, if you’d only like to change<br />

the element, the entire XML document is actually replaced.<br />

UPDATE clients SET contact=(<br />

xmlparse(document<br />

‘<br />

<br />

5401 Julio ave.<br />

San Jose<br />

CA<br />

95116<br />

<br />

<br />

4084633000<br />

4081111111<br />

4082222222<br />

<br />

4087776666<br />

newemail@someplace.com<br />

')<br />

)<br />

WHERE id = 3227<br />

Listing 15.25 - An example of an SQL UPDATE<br />

For the second way, you can perform sub-document updates using the TRANSFORM<br />

expression, which is a lot more efficient. This allows you to replace, insert, delete or<br />

rename nodes in an XML document. You can also change the value of a node <strong>with</strong>out<br />

replacing the node itself, typically to change an element or attribute value–which is a very<br />

common type of update. This support was added in <strong>DB2</strong> 9.5.<br />

The TRANSFORM expression is part of the XQuery language, you can use it anywhere<br />

you normally use XQuery, for example in a FLWOR expression or in the XMLQUERY<br />

function in an SQL/XML statement. The most typical use is in an SQL UPDATE statement<br />

to modify an XML document in an XML column.<br />

Listing 15.26 shows the syntax of the TRANSFORM expression.<br />

>>-transform--| copy clause |--| modify clause |--| return clause |-><<br />

copy clause

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!