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.

276 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>DB2</strong> <strong>Express</strong>-C<br />

)<br />

Listing 15.7 - An example using XMLEXISTS<br />

In Listing 15.7, the first line is an SQL clause specifying that you want to retrieve<br />

information in the name column of the clients table.<br />

The WHERE clause invokes the XMLEXISTS function, specifying the XPath expression<br />

that prompts <strong>DB2</strong> to navigate to the zip element and check for a value of 95116<br />

The $c/Client/Address clause indicates the path inside the XML document hierarchy<br />

where <strong>DB2</strong> can locate the zip element. A dollar sign ($) is used to specify a variable;<br />

therefore “c” is a variable. This variable is then defined by this line: passing<br />

clients.contact as "c". Here, clients is the name of the table and contact is the<br />

name of the column <strong>with</strong> an XML data type. In other words, we are passing the XML<br />

document to the variable “c”.<br />

<strong>DB2</strong> inspects the XML data contained in the contact column, navigates from the root<br />

Client node down to the Address node, then to the zip node and finally determines if<br />

the customer lives in the target zip code. The XMLEXISTS function evaluates to “true” and<br />

<strong>DB2</strong> returns the name of the client associated <strong>with</strong> that row.<br />

Starting <strong>with</strong> <strong>DB2</strong> 9.5, the above query could be simplified as shown in Listing 15.8 below.<br />

SELECT name FROM clients<br />

WHERE xmlexists(<br />

'$CONTACT/Client/Address[zip="95116"]'<br />

)<br />

Listing 15.8 - Simplified version of the query shown in Listing 15.7<br />

A variable <strong>with</strong> the same name as an XML column is created automatically by <strong>DB2</strong>. In the<br />

above example, the variable CONTACT is created automatically by <strong>DB2</strong>. Its name matches<br />

the name of the XML column CONTACT.<br />

Example 2<br />

Let’s consider how to solve the problem of how to create a report listing the e-mail<br />

addresses of “Gold” status customers. The query in Listing 15.9 below could be run for this<br />

purpose.<br />

SELECT xmlquery('$c/Client/email' passing contact as "c")<br />

FROM clients<br />

WHERE status = 'Gold'<br />

Listing 15.9 - An example using XMLQUERY<br />

The first line indicates we want to return the email address which is an element of the XML<br />

document (not a relational column). As in the previous example, “$c” is a variable that<br />

contains the XML document. In this example we use the XMLQUERY function which can<br />

be used after a SELECT, while the XMLEXISTS function can be used after a WHERE<br />

clause.

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

Saved successfully!

Ooh no, something went wrong!