Stored Procedure to insert data to parent and child using XML

Couple of days back I wrote an article that showed how to quickly insert a child table with a  parent table having generated column. On this one I am going to show you how to insert data using XML as a value.  As my previous article show its easy to insert to child table , but you have to insert one row at a time. Well what if we have multiple child rows that needs to be inserted . Here I am going to show you how to do that using XML as an input string holding child data. To make it nice and tight I wrote a small stored procedure . First lets create the tables (its the same set of tables I had it on my previous post) :

create table orders
(order_id  int not null
generated always as identity (start with 1000,increment by 1, nocache)
,customer_id int not null
,order_timestamp  timestamp
,order_channel_id  varchar(5)
,csr_id  varchar(10)
,primary key (order_id)
create table order_items
(order_id  int not null
,seq_num  int not null
,product_id  varchar(20) not null
,qty int
,unit_price dec(9,2)
,product_discount dec(9,2)
,primary key(order_id,seq_num)

Now that the table is created, lets create the stored procedure that will insert the data to the parent table and all data to the child table. Granted this stored procedure does not have all the bells and whistles and is only intended for demonstrating a method.

create or replace procedure spIorders
(in customerid int
,in orderchannelid varchar(5)
,in csrid varchar(10)
,in orderitems XML
,out orderid int
— declaring global temporary table to hold the data
declare global temporary table session.gtt_orders
(customer_id int not null
,order_channel_id varchar(5)
,csr_id varchar(10)
,order_item XML)
on commit delete rows
not logged;
— insert the data to the global temporary table from the data passed in
insert into session.gtt_orders
–insert into parent table (orders) from global temporary table
select order_id into orderid
from new table
(insert into orders
select gtt.customer_id,current_timestamp,gtt.order_channel_id,gtt.csr_id
from session.gtt_orders gtt);
–insert into child table (order_items) from global temporary table
–xmltable will split the XML document to multiple rows
–row_number() over() is an olap function that will generate rows number for seq_num
insert into order_items
select orderid,row_number() over(),x.productid,x.qty,x.unitprice,x.productdiscount
from session.gtt_orders gtt,
xmltable(‘$c/orderitem/item’ passing gtt.order_item as “c”
productid varchar(20) path ‘productid’,
qty int path ‘qty’,
unitprice dec(9,2) path ‘unitprice’,
productdiscount dec(9,2) path ‘productdiscount’) as x;

— dropping the global temporary table since the inserts have be done.
drop table session.gtt_orders;

Now lets call the stored procedure we just created an pass in an XML that has three order items :

call spIorders (5000,’Web’,’RAJU’,
‘ preserve whitespace),?);

If you query the orders and order_items you should be able to see one row in orders table and three rows in order_items.
Please feel free to comment on this and possible improvements or alternate methods.


About rpillai

I am a technology enthusiasts and love to work with databases and other technology. Learning new things everyday and don't think the path ever ends ...
This entry was posted in db2, how to and tagged , , , , , , , , , , , . Bookmark the permalink.