16.10.2015 Views

Getting Started with DB2 Express-C

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

where $y/ResponseRequested=‘Yes’<br />

return (<br />

<br />

{$y/ProductID<br />

$y/CustomerID<br />

$y/Message}<br />

<br />

)<br />

Listing 15.19 - An example of the db2-fn:sqlquery function embedding SQL <strong>with</strong>in<br />

XQuery<br />

In the example, the SQL query filters rows based on the condition that the srp column has<br />

a value greater than 100. From those rows filtered, it will pick the comments column,<br />

which is the XML column. Next XQuery (or XPath) is applied to go to sub-elements.<br />

Note:<br />

SQL is case insensitive and <strong>DB2</strong> stores all table and column names in uppercase by<br />

default. XQuery on the other hand, is case sensitive. The above functions are XQuery<br />

interface functions so all the table names and column names should be passed to these<br />

functions in uppercase. Passing the object names in lowercase may result in an undefined<br />

object name error.<br />

15.3.6 Joins <strong>with</strong> SQL/XML<br />

This section describes how to perform JOIN operations between two XML columns of<br />

different tables, or between one XML column and one relational column. Assume you have<br />

created two tables <strong>with</strong> the statements shown in Listing 15.20<br />

CREATE TABLE dept (unitID CHAR(8), deptdoc XML)<br />

CREATE TABLE unit (unitID CHAR(8) primary key not null,<br />

name CHAR(20),<br />

manager VARCHAR(20),<br />

...<br />

)<br />

Listing 15.20 - DDL of tables to use in the JOIN examples<br />

You can perform a JOIN operation in either of two ways. The first method is shown in<br />

Listing 15.21.<br />

SELECT u.unitID<br />

FROM dept d, unit u<br />

WHERE XMLEXISTS (<br />

‘$e//employee[name = $m]’<br />

passing d.deptdoc as “e”, u.manager as “m”)<br />

Listing 15.21 - First method to perform a JOIN <strong>with</strong> SQL/XML

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

Saved successfully!

Ooh no, something went wrong!