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 271<br />

<br />

Listing 15.2 - A sample XML document<br />

Then the XQuery statement in Listing 15.3 using the FLWOR expression could be run:<br />

xquery<br />

for $d in db2-fn:xmlcolumn('dept.deptdoc')/dept<br />

let $emp := $d//employee/name<br />

where $d/@bldg > 95<br />

order by $d/@bldg<br />

return<br />

<br />

{$d/@bldg, $emp}<br />

<br />

Listing 15.3 - A sample XQuery statement <strong>with</strong> the FLWOR expression<br />

This would return the output shown in Listing 15.4<br />

<br />

<br />

John Doe<br />

<br />

<br />

Peter Pan<br />

<br />

<br />

Listing 15.4 - Output after running the XQuery statement in Listing 15.3<br />

15.3.4 Inserting XML documents<br />

Inserting XML documents into a <strong>DB2</strong> database can be performed using the SQL INSERT<br />

statement, or the IMPORT utility. XQuery cannot be used for this purpose as this has not<br />

yet been defined in the standard.<br />

Let’s examine the script table_creation.txt shown in Listing 15.5 below, which can<br />

be run from the <strong>DB2</strong> Command Window or Linux shell using this statement:<br />

db2 –tvf table_creation.txt<br />

-- (1)<br />

drop database mydb<br />

;<br />

-- (2)<br />

create database mydb using codeset UTF-8 territory US<br />

;<br />

-- (3)<br />

connect to mydb

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

Saved successfully!

Ooh no, something went wrong!