Need help on Db2 Stored procedures

Hi ,

Iam new to Db2 , I need help in writing an stored procedure that accepts an xml file and read the xml file and insert into DB table called Account

<?xml version="1.0" encoding="UTF-8"?>
<Account>
<Name>sam</Name>
<Age>29</Age>
<Gender>M</Gender>
</Account>

DB table : Account

Name varchar
Age Integer
Gender char

Any help is really appreciated as Iam not aware of DB2 .
sam_2012Asked:
Who is Participating?
 
Kent OlsenData Warehouse Architect / DBACommented:
If it was my application, I'd write one something like this:

CREATE PROCEDURE load_xml_file (IN in_filename VARCHAR(255))
LANGUAGE SQL
BEGIN
DECLARE cmd VARCHAR(1000);
 
SET cmd = 'LOAD FROM {testfile} of del XMS from ' || in_filename ||
  ' INSERT INTO {xml_table}';
 
 CALL SYSPROC.ADMIN_CMD(cmd);
END

Open in new window


Substitute your values for {testfile} and {xml_table}.

Also note that stored procedures run on the server, NOT the client machine.  The file must be accessible by the server.


Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Sam,

You can parse the XML and manage the logic yourself or you can use the DB2 utilities to load the data into an appropriate table for you.  The easiest way to get an XML document (file) into DB2 is with the LOAD or IMPORT function.  (They are both part of the db2 command line processor.)

You should be able to execute the LOAD or IMPORT by calling SYSPROC.ADMIN_CMD.


Good Luck,
Kent
0
 
sam_2012Author Commented:
Hi kent,

I have been asked to do it using the xml functions available in Db2  and also they want me to write an procedure  instead of load or import utility as this will be called from an application.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Sam,

The IBM documentation on handling XML in DB2 is quite lengthy.  Probably more than you care to read in a week.  So I suggest you start smaller and review this page:

 https://amolnpujari.wordpress.com/2008/04/12/db2-native-xml/

It briefly explains a lot of what you can do in XML and is much more concise than anything that I can write up in just a few minutes.  Please review that page and get back to me.  We'll see where to go next.

Kent
0
 
sam_2012Author Commented:
that link was good example on XML. But iam too sure how to code using Db2 stored procedure , any example of an stored procedure to read the xml file and insert into table will be really useful.
0
 
sam_2012Author Commented:
awesome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.