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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
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
Kent OlsenDBACommented:
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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
Kent OlsenDBACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sam_2012Author Commented:
awesome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.