You are on page 1of 4

<customerinfo>

<name></name>
<addr></addr>
<phone></phone>
</customerinfo>

create table xmlcustomer (cid bigint,


info XML)
insert into xmlcustomer values
(1000,'<customerinfo><name>Chetan
N</name><addr>Pune</addr><phone>963-2894136</phone></customerinfo>')
insert into xmlcustomer values (2000,'<customerinfo><name>Shailesh
N</name><addr>Mumbai</addr><phone>992-3745216</phone></customerinfo>')

1. SELECT * FROM XMLCUSTOMER


2. search based on particular tag name

SELECT cid FROM xmlcustomer


WHERE

XMLEXISTS('$h/customerinfo/name'
PASSING info AS "h")
3.

Search specific tag data

select
xmlquery('$c/customerinfo/phone'
passing info as "c") from xmlcustomer
where cid=1000 //
4.

Update specific tag value

update xmlcustomer
set info = xmlquery('transform copy $new := $INFO modify do replace value of
$new/customerinfo/phone with "905-477-9011" return $new')
where cid = 1000

5. Delete specific XML Tag


update xmlcustomer set info = xmlquery( 'copy $new := $INFO modify do delete
$new/customerinfo/phone return $new' ) where cid = 1000

XML Information
select name from clients
where xmlexists('$c/Client/Address[zip="95116"]'
passing clients.contactinfo as "c")
The first line is an SQL clause specifying that you only want to retrieve
information in the name column of the clients table. The WHERE clause
invokes the XMLExists function, specifying an XPath expression that prompts
DB2 to navigate to the zip element and check for a value of 95116. The
$c/Client/Address clause indicates the path in the XML document hierarchy
where DB2 can locate the zip element. Using data accessible from node $c
(which we'll explain shortly), DB2 will navigate through the Client element to its
Address sub-element to inspect zip code (zip values). The final line resolves
the value of $c: it's the contactinfo column of the clients table. Thus, DB2
inspects the XML data contained in the contactinfo column, navigates from the
root Client element to Address and then to zip, and determines if the
customer lives in the target zip code. If so, the XMLExists function evaluates to
true, and DB2 returns the name of the client associated with that row.
Now let's consider a slightly different situation, in which you want to project XML
values into your returned result set. In other words, we want to retrieve one or
more element values from our XML documents. There are multiple ways to do
this. Let's first use the XMLQuery function to retrieve a value for one element,
and then use the XMLTable function to retrieve values for multiple elements and
map these into columns of an SQL result set.
Let's consider how to solve a problem posed earlier: how to create a report listing
the e-mail addresses of the Gold customers. The following query in Example 4-7
invokes the XMLQuery function to accomplish this task.
Example 4-7 Retrieving e-mail information for qualifying customers

select xmlquery('$c/Client/email'
passing contactinfo as "c")
from clients
where status = 'Gold'
The first line specifies that you want to return values for the email sub-element
of the root Client element. The second and third lines indicate where DB2 can
find this information in the contactinfo column of the clients table. The fourth
line further qualifies your query to indicate that you're only interested in e-mail
addresses of Gold customers. This query will return a set of XML elements and
values. For example, if you had 500 Gold customers, each with one e-mail
address, your output would be a one-column result set with 500 rows, as shown

You might also like