+ All Categories
Home > Documents > DB2 Developer Workbench, Part 3: Developer Workbench and XML

DB2 Developer Workbench, Part 3: Developer Workbench and XML

Date post: 29-Jan-2015
Category:
Upload: tess98
View: 141 times
Download: 0 times
Share this document with a friend
Description:
 
Popular Tags:
42
DB2 Developer Workbench, Part 3: Developer Workbench and XML Basic tasks Skill Level: Intermediate Debra R. Eaton ([email protected]) DB2 Software Information Technology Specialist IBM 19 Oct 2006 The IBM® DB2® Developer Workbench (DWB) provides out-of-the-box integrated development for DB2 9 pureXML. DWB is based on the Eclipse open source Integrated Development Environment (IDE). Learn how the DWB resources, perspectives, views, editors, and wizards assist you to work with the XML functionality in DWB. Section 1. Before you start About this tutorial This tutorial takes a basic approach to working with XML in the DWB Eclipse environment, starting by creating an XML table, schema and document, followed by querying XML data with a stored procedure. Objectives Learn how the DWB resources, perspectives, views, editors, and wizards assist you to work with XML. Become familiar with tasks such as creating, registering, and editing XML schemas, creating XML instance documents from schemas, creating XML-related database objects such as tables that contain XML data Developer Workbench and XML © Copyright IBM Corporation 1994, 2007. All rights reserved. Page 1 of 42
Transcript
Page 1: DB2 Developer Workbench, Part 3: Developer Workbench and XML

DB2 Developer Workbench, Part 3: DeveloperWorkbench and XMLBasic tasks

Skill Level: Intermediate

Debra R. Eaton ([email protected])DB2 Software Information Technology SpecialistIBM

19 Oct 2006

The IBM® DB2® Developer Workbench (DWB) provides out-of-the-box integrateddevelopment for DB2 9 pureXML. DWB is based on the Eclipse open sourceIntegrated Development Environment (IDE). Learn how the DWB resources,perspectives, views, editors, and wizards assist you to work with the XMLfunctionality in DWB.

Section 1. Before you start

About this tutorial

This tutorial takes a basic approach to working with XML in the DWB Eclipseenvironment, starting by creating an XML table, schema and document, followed byquerying XML data with a stored procedure.

Objectives

• Learn how the DWB resources, perspectives, views, editors, and wizardsassist you to work with XML.

• Become familiar with tasks such as creating, registering, and editing XMLschemas, creating XML instance documents from schemas, creatingXML-related database objects such as tables that contain XML data

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 1 of 42

Page 2: DB2 Developer Workbench, Part 3: Developer Workbench and XML

columns, and creating queries for XML data.

Prerequisites

You must install and configure the SAMPLE database that is included with DB2 9.1.See the DB2 product documentation and First Steps for more information. Ensurethe SAMPLE database is XML-enabled by specifying the XML option at databasecreation.

See Resources for links to install DB2 and the Developer Workbench.

System requirements

To run the examples in this tutorial, you need to install DB2 9.1 and the SAMPLEdatabase. You must be able to connect to the SAMPLE database with a user ID andpassword.

The schema used throughout this tutorial is DEATON. Replace the DEATONschema with your schema, represented in the instructions by SCHEMANAME, whena task requires a schema name.

Section 2. Set up the Data perspective

Introduction

This section steps you through the initial tasks of working with the Data perspectivein DWB. In this section, accomplish the following tasks:

• Start DWB

• Verify the Data perspective

• Connect to a database

• Create a data development project

Start DWB

From the Windows desktop, select Start > All Programs > IBM DB2 DeveloperWorkbench > DB2 Developer Workbench 9.1 > DB2 Developer Workbench.Click OK to pick the default location. After you choose the workspace location, asingle Workbench window displays. A Workbench window offers one or more

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 2 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 3: DB2 Developer Workbench, Part 3: Developer Workbench and XML

perspectives that contain editors and views.

Note: The first time the workspace is used, the DWB welcome screen is displayed.You then have to click on the arrow in the upper, right-hand corner (the Workbenchlink) to get to the Workbench, where you are shown the Data perspective. You canlaunch this welcome page using the help menu.

Figure 1. Start DWB

Verify the Data perspective

DWB opens by default in the Data perspective. Data will appear on the title bar inthe upper left. The Data icon will be indented on the icon bar in the upper right.

Figure 2. Data perspective

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 3 of 42

Page 4: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Connect to a database

DWB lists all known database connections in the Database Explorer view.Right-click the SAMPLE database connection in the Database Explorer Connectionsfolder, and select Reconnect from the context menu.

Note: You can create more database connections that the DB2 server does notknow about with the Create Connection wizard.

.Figure 3. New connection

In the Database Authorization window, enter your database user ID and password.Select OK.

Figure 4. Database authorization

Create a project

In the Data Project Explorer view, right-click on the white space within the view.Select New >Project. The New Data Development Project wizard will open.

Figure 5. New project

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 4 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 5: DB2 Developer Workbench, Part 3: Developer Workbench and XML

On the Select a wizard step, expand the Data folder. Select the Data DevelopmentProject option. Click Next.

Figure 6. New project wizard selection

In the Project name field, type XMLBasic. Click Next

Figure 7. Project name

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 5 of 42

Page 6: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Select Use an existing connection. Select SAMPLE in the Existing connectionslist box. Click Next.

Figure 8. New project connection

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 6 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 7: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Specify the JDK home directory in the JDK home text box, and click Finish. Thedata development project called XMLBasic is displayed in the Data Project Explorerview.

Figure 9. New project JDK

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 7 of 42

Page 8: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Section 3. Create a table

Introduction

You can create a table in DWB with the SQL editor. In this section, accomplish thefollowing tasks:

• Create table XMLSAMPLE in the SAMPLE database

• Define column DOC as an XML data type

Create a table

In the Database Explorer view, expand the SAMPLE database schema tree.Right-click the Tables node. Select New > With SQL Editor to launch the SQLEditor.

Figure 10. Launch SQL Editor

In the SQL Editor, enter the CREATE TABLE statement to create a table calledXMLSample, which includes one XML column called doc. Add a DROP TABLEstatement to remove a previous version of XMLSample.

Figure 11. Create a table in the SQL Editor

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 8 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 9: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Click the Save icon on the task bar. In the Save As window, select the XMLBasicparent folder. Type create XMLSample.sql in the File Name text box. Click OK.

Figure 12. Save create XMLSample.sql script

In the Data Project Explorer view, in the XMLBasic project SQL Scripts folder, selectthe script create XMLSample.sql. Right-click on it, and select Run SQL.

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 9 of 42

Page 10: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Figure 13. SQL statement

On the Data Output view, verify the script create XMLSample.sql ran successfully.You should see Status = Success, Action = Run, and Object Name = SQL Editor. Inthe Messages tab, you should see the message Run Successful. On theDatabase Explorer view, right-click the SAMPLE node. Select Refresh. Expand theSAMPLE database schema tree. Verify the XMLSample table exists in the Tablesnode.

Figure 14. SELECT

Section 4. Create an XML schema

Introduction

You can create an XML schema in DWB with the XML Schema Editor. The XMLSchema Editor has two views: the Source view shows the schema source codeinside a text editor, and the Graph view shows a graphical representation of theschema. Tabs at the bottom of the XML Schema Editor enable switching betweenthe two views. In the Graph view, add new global elements and attributes with thecontext menu options. Drill down into element hierarchies by double clicking on theelement. The context menu adds and modifies elements in the XML schemastructure.

In this section, accomplish the following tasks:

• Create schema SAMPLESCHEMA

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 10 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 11: DB2 Developer Workbench, Part 3: Developer Workbench and XML

• Define a complex type element called CUSTOMERINFO

• Define an element sequence consisting of NAME, CITY, and ZIP inCUSTOMERINFO

Create a schema

In the Data Project Explorer view, expand the XMLBasic project tree. Right-click theXML Schema Documents node. Select New to launch the Create XML Schemawizard.

Figure 15. Launch the Create XML Schema wizard

In the New XML Schema window, select the XMLBasic parent folder. TypeSampleSchema.xsd in the File Name text box. Click Finish.

Figure 16. New schema name

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 11 of 42

Page 12: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the XML Schema Editor, select the Graph tab at the bottom of the editor to switchto the graphical view of the XML Schema. Right-click the white space in the Elementpane. Select Add Element.

Figure 17. Add an element in the XML Schema Editor

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 12 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 13: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Type customerinfo in the black highlighted element. Right-click the customerinfoelement. Select Set Type > New Complex Type.

Figure 18. Set element type

Double-click the customerinfo element to create an element hierarchy. To the rightof the customerinfo box, appears a sequence box.

Figure 19. Create a sequence box

Right-click the sequence box. Select Add Element.

Figure 20. Create a sequence box

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 13 of 42

Page 14: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Change the element name to Name.

Figure 21. Create the Name element

Add the element City and Zip to the customerinfo type using the same process aswhen you added Name. Verify the elements in the XML Schema Editor graphicalview and the Outline view.

Figure 22. Verify elements in the XML schema graphical view and Outline view

Select the Source tab in the XML Schema Editor. Find the customerinfo, Name,City, and Zip elements in the schema.

Figure 23. Verify the XML schema in the XML Schema Editor

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 14 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 15: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Click the Save icon on the task bar.

Figure 24. Save icon

Section 5. Register an XML schema

Introduction

You can register an XML schema to the XML Schema Repository with the DWBRegister XML Schema Wizard. XML schemas need to be registered in the XMLSchema Repository (XSR) so the database can decompose (shred) and validateXML data. A user can manage XML schema documents and XML schemas in theXSR by viewing their properties and dropping them from the server when they areno longer needed.

In order to register an XML schema, you must have all the documents in yourproject. For multi-document schemas with dependencies, the schema location in thedocuments must be the filename of the documents.

You can use registered XML schemas to perform validation testing in the TableEditor. When you insert or modify a value for an XML column, you can specify thatthe document is to be validated, either based on information in the document thatindicates the schema which it conforms to, or a schema specified by the user.

In this section, accomplish the following task:

• Register SAMPLESCHEMA in the SAMPLE schema repository

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 15 of 42

Page 16: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Register an XML schema

In the Data Project Explorer view, expand the XMLBasic project tree. Right-click theSampleSchema node. Select Register an XML Schema wizard to launch theRegister XML Schema wizard.

Figure 25. Launch the Register XML Schema Wizard

In the Relational Schema Name and XML Schema window, select the DEATONrelational schema name. Type SampleSchema in the XML Schema Name text box.Click Next.

Figure 26. Relational Schema Name and XML Schema Name

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 16 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 17: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the XML Schema Documents window, select the SampleSchema.xsd XMLschema document. Click Finish.

Figure 27. XML schema document

On the Data Output view, verify the XML schema registration ran successfully. You

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 17 of 42

Page 18: DB2 Developer Workbench, Part 3: Developer Workbench and XML

should see Status = Success, Action = Register, and Object Name =SAMPLESCHEMA. In the Messages tab, you should see the message RegisterSuccessful. On the Database Explorer view, right-click the SAMPLE node. SelectRefresh. Expand the SAMPLE database schema tree. Verify the SAMPLESCHEMAexists in the XML Schemas node.

Figure 28. Schema registration success verification

Section 6. Generate an XML document from an XMLschema

Introduction

Use the Navigator View to generate a sample XML document from an XML schema.In this section, accomplish the following task:

• Generate XML document SAMPLESCHEMA.xml

Generate an XML document from an XML schema

Select the workbench window menu options Window > Show View > Navigator. Ifthe Navigator view opens in the bottom, right area of DWB, drag the Navigatorwindow on to the Data Project Explorer window.

Figure 29. Open the Navigator view

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 18 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 19: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the Navigator view, expand the XMLBasic project tree. Right-clickSampleSchema.xsd. Select Generate > XML File to launch the Create XML filedialog. Click Next.

Figure 30. Generate XML File context menu options

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 19 of 42

Page 20: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Select customerinfo in the root element dropdown list. Click Finish. ASampleSchema.xml file is generated from the SampleSchema.xsd file. TheSampleSchema.xml file opens in the XML editor in the Source view, which consistsof a text editor with color coding.

Figure 31. Select the root element

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 20 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 21: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Section 7. Edit an XML document

Introduction

Use the XML Editor to edit an XML document. In this section, accomplish thefollowing tasks:

• Edit XML document SAMPLESCHEMA.xml

• Alter element text values

Edit an XML document

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 21 of 42

Page 22: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the XML Editor, select the Design tab to switch to the graphical view of theSampleSchema.xml document hierarchy.

Note: You can launch the XML Editor by opening the Navigator view, right-clickingon the XML file, and selecting Open With > XML Editor.

Figure 32. XML Editor Source view of an XML document

In the Design view of the XML Editor, expand the customerinfo element. Select theName value in the grid box next to the Name element. Enter Smith. Select the Cityvalue in the grid box next to the City element. Enter Chicago. Select the Zip valuein the grid box next to the Zip element. Enter 60611. Click the Save icon on the taskbar.

Figure 33. Enter element values

Section 8. Insert an XML document into an XML column

Introduction

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 22 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 23: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Use the XML Cell Editor to insert the SampleSchema.xml XML document into theDOC column in the XMLSample table. In this section, accomplish the followingtasks:

• Validate XML document SAMPLESCHEMA.XML with XMLVALIDATE

• Insert SAMPLESCHEMA.XML into XML column DOC in tableXMLSAMPLE

• View SAMPLESCHEMA.XML in table XMLSAMPLE

Insert an XML document into an XML column

In the Database Explorer view, expand the SAMPLE database schema tree.Right-click the XMLSAMPLE table node. Select Data > Edit to launch the TableData Editor.

Figure 34. Launch the Table Data Editor

The last row of the table data editor has a new row in the XML column. Click on theellipses button to launch the XML Cell Editor. Select Import to import the XMLdocument from a file system.

Figure 35. XML Cell Editor new row

Select SampleSchema.xml in the XMLBasic folder. Select Open.

Figure 36. Import XML file dialog

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 23 of 42

Page 24: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Observe the XML document in the Source view. Select the Validate XML documentoption. Click Next.

Figure 37. Validate an XML document option

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 24 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 25: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Select the Specify a registered XML schema option. Select SAMPLESCHEMAfrom the menu options. Click Finish.

Figure 38. Specify a registered XML schema

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 25 of 42

Page 26: DB2 Developer Workbench, Part 3: Developer Workbench and XML

The XML document is added as a row in the Table Data Editor.

Figure 39. New row in Table Data Editor

Click the Save icon on the task bar. The row is validated against the registeredschema called SampleSchema and inserted into the XMLSAMPLE table. On theData Output view, verify the INSERT and XMLVALIDATE statements ransuccessfully. You should see Status = Success, Action = Edit, and Object Name =XMLSAMPLE. In the Messages tab, you should see the message Inserted 1row(s).

Figure 40. New row Data Output view

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 26 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 27: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the Database Explorer view, expand the SAMPLE database schema tree.Right-click the XMLSAMPLE table node. Select Data > SAMPLE Contents toretrieve sample data.

Figure 41. New row Data Output view

On the Data Output view, verify sample data was returned. You should see Status =Success, Action = Run, and Object Name = Sample Contents. In the Results tab,you should see one row returned. Click on the ellipses button to launch the sourcewindow. Select XML Tree to see a graphical view of the SampleSchema XMLdocument stored in the XMLSAMPLE table.

Figure 42. New row Data Output view

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 27 of 42

Page 28: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Section 9. Create an XQuery

Introduction

Use XQuery Builder to create an XQuery. The XQuery Builder interprets user GUIactions into queries without exposing you to language semantics. The XQueryBuilder tool creates a tree view of an instance of the XML document, which will bequeried. You can drag and drop nodes from the node tree view to the design grid(s).

The design grid(s) are context sensitive and represent different XQuery languagefunctions. Build the XQuery by adding resources to these grids and drilling into theseresources. Different resources represent different functionality in the XQuerylanguage and thus present different grid sets. The query is internally represented asan XML model. Each resource added into the grid is added to the XML model.

You must associate a representative sample document used to build the XQuerywith an XML column in the table. The instance of the XML document can be locatedin the file system or in the database from an XML column in a table. Dragging and

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 28 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 29: DB2 Developer Workbench, Part 3: Developer Workbench and XML

dropping nodes from the sample XML document tree to a row on the design gridresults in the creation of an XPath object in the XQuery model. This XPath objectrepresents the node path in the target XML document.

XQuery is passed to DB2 using the XQuery language keyword XQUERY or theSQL/XML language function XMLQUERY. Only the SQL/XML language functionXMLQUERY allows runtime parameters to be passed into the XQuery. Since mostqueries contain predicates that are populated at runtime, the XQuery Builder onlygenerates SQL/XML language queries. Also, SQL/XML queries are easy to embedin SQL statements created with the SQL Query Builder.

In this section, accomplish the following tasks:

• Select representative XML document SAMPLESCHEMA.XML

• Associate representative XML document SAMPLESCHEMA.XML withXML column DOC in table XMLSAMPLE

• Build XMLQuery statement in the XQuery Builder

• Run the XMLQuery statement

• View the query output

Create an XQuery

In the Data Project Explorer view, expand the XMLBasic project tree. Right-click theXML Queries node. Select New XML Query to launch the XQuery Builder.

Figure 43. Launch the XQuery Builder

Type the name SampleQuery in the File name text box, then click Next.

Figure 44. Define a XML Query name

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 29 of 42

Page 30: DB2 Developer Workbench, Part 3: Developer Workbench and XML

To build an XQuery, use sample XML documents to represent the XML hierarchicalstructure. The representative XML document can be loaded from the file system orfetched from the database. You will load the document from the file system. ClickAdd. Select the Local workspace option. Select Browse, then choose theSampleSchema.xml. Click OK. Click Finish.

Figure 45. Select the representative document

SampleSchema.xml is the representative document. Click Next.

Figure 46. View the representative document

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 30 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 31: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Representative documents must be associated with XML columns so that the queryis built to obtain data from those columns. Select SampleSchema.xml. ClickAssociate.

Figure 47. Launch associate

Expand XMLSAMPLE table node. Select DOC. Click OK.

Figure 48. Select the DOC XML column

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 31 of 42

Page 32: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Verify the XML document SampleSchema.xml is associated with the DOC XMLcolumn in the XMLSAMPLE table. Click Finish.

Note: The Omit Schema check box results in the Relational schema (not XMLschema) being omitted from the XQuery.

Figure 49. Finish the association

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 32 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 33: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Drag and drop the customerinfo node from the SampleSchemal.xml tree view to arow on the design grid. The node name appears in the grid and a drill down buttonappears at the end of the row.

Figure 50. Drag a resource onto the grid

Click Step into at the end of the customerinfo row in the design grid. If the Step intobutton is not visible, select the customerinfo row. The FLWOR grid appears. Thevariable customerinfo, which contains the original XPath object that represented thecustomerinfo node in the sample XML document, is created and populated in theFLWOR grid.

Figure 51. Drill into a resource

Add a WHERE predicate to the FOR logic. Drag and drop the Name element from theSampleSchema.xml tree view to the Operand1 column in the where grid. Click theOperator column in the where grid to display a list of all comparison operatorspossible in the WHERE clause. Select the = operator. Enter Smith in the Operand2column in the where grid. Click the Save icon on the task bar.

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 33 of 42

Page 34: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Figure 52. Drill into a resource

Click the Source tab, and view the generated query. Expand the outline view of theXQuery to view the query in a graphical format.

Figure 53. View query in Source and Outline views

Select XQuery > Run on the menu bar to execute the query. Click Finish.

Figure 54. XQuery context menu

On the Data Output view, verify the query ran successfully. You should see Status =Success, Action = Run, and Object Name = SampleQuery.xqm. In the Results tab,you should see one document returned. Click on the ellipses button to launch thesource window. Select XML Tree to see a graphical view of the SampleSchemaXML document stored in the XMLSAMPLE table.

Figure 55. Drill into a resource

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 34 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 35: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Section 10. Create a stored procedure for XML data

Introduction

Use the Stored Procedure Wizard to create a stored procedure that contains anXQuery. Stored procedures created in either SQL or Java can return result sets thatcontain XML columns. In this section, accomplish the following tasks:

• Create a stored procedure

• Import an XQuery

• Run the stored procedure

Create a stored procedure for XML data

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 35 of 42

Page 36: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the Data Project Explorer view, expand the XMLBasic project tree. Right-click theStored Procedures folder. Select New > Stored Procedure to launch the StoredProcedure Wizard. Click Next to continue.

Figure 56. Launch the Stored Procedure Wizard

In the Name and Language window, type XMLSP in the Name text box. Click Next.

Figure 57. Name and language

Select Import to import the SampleQuery.xqm SQL query statement from the

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 36 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 37: DB2 Developer Workbench, Part 3: Developer Workbench and XML

XMLBasic project. Select SampleQuery from the options tree. Select Import.

Figure 58. Import an XML statement

Select Statement 1 in the Statements pane. Select Remove to delete the existingSELECT statement. Click Next. Click Next on the Parameters window.

Figure 59. Remove a statement

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 37 of 42

Page 38: DB2 Developer Workbench, Part 3: Developer Workbench and XML

In the Deploy Options window, type XMLSP1 in the Specific name text box. Selectthe Deploy check box. Click Next. Click Next on the Code fragments window. ClickFinish on the Summary window.

Figure 60. Stored procedure deploy options

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 38 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 39: DB2 Developer Workbench, Part 3: Developer Workbench and XML

The stored procedure XMLSP is built and deployed to the database. XMLSPappears under the Stored Procedures folder in the Data Project Explorer view. TheCREATE PROCEDURE statement appears in the XMLSP editor. Check for asuccessful deploy of the XMLSP stored procedure in the Data Output view.

Figure 61. XMLSP editor

In the Database Project Explorer view, select the XMLSP stored procedure underthe Stored Procedures folder in the XMLBasic project. Right-click, and select theRun menu option.

Figure 62. Run a stored procedure

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 39 of 42

Page 40: DB2 Developer Workbench, Part 3: Developer Workbench and XML

On the Data Output view, verify that the stored procedure ran successfully. Youshould see Status = Success, Action = Run, and Object Name = XMLSP. In theResults tab, you should see one document returned. Click on the ellipses button tolaunch the source window. Select XML Tree to see a graphical view of theSampleSchema XML document stored in the XMLSAMPLE table.

Figure 63. Stored Procedure Output

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 40 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.

Page 41: DB2 Developer Workbench, Part 3: Developer Workbench and XML

ibm.com/developerWorks developerWorks®

Developer Workbench and XML© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 41 of 42

Page 42: DB2 Developer Workbench, Part 3: Developer Workbench and XML

Resources

Learn

• developerWorks resource page for DB2 for Linux, UNIX, and Windows: Findarticles and tutorials and connect to other resources to expand your DB2 skills.

• developerWorks Information Management zone: Expand your skills on IBMInformation Management products.

• Learn about DB2 Express-C, the no-charge version of DB2 Express Edition forthe community.

• DB2 training sites list several learning delivery methods for the use of DB2. .

• Stay current with DB2 events atvia developerWorks technical events andWebcasts.

Get products and technologies

• Download a free trial version ofDB2 9.1.

• Now you can use DB2 for free. Download DB2 Express-C, a no-charge versionof DB2 Express Edition for the community that offers the same core datafeatures as DB2 Express Edition and provides a solid base to build and deployapplications.

• Download the DB2 Developer Workbench.

Discuss

• Participate in the discussion forum for this content.

• Participate in developerWorks blogs and get involved in the developerWorkscommunity.

About the author

Debra R. EatonDebra Eaton has worked in DB2 Technical Sales as a Software InformationTechnology Specialist on the DB2 Migration Team for 10 years. She specializes inconverting customer applications from non-DB2 databases to DB2. She has authoredIBM Redbooks, white papers, and DB2 Magazine articles on topics related to DB2application development. In addition, she has presented these topics at IDUG andDB2 technical conferences.

developerWorks® ibm.com/developerWorks

Developer Workbench and XMLPage 42 of 42 © Copyright IBM Corporation 1994, 2007. All rights reserved.


Recommended