{"id":1318,"date":"2012-05-25T20:22:16","date_gmt":"2012-05-25T14:22:16","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1318"},"modified":"2016-01-07T11:31:04","modified_gmt":"2016-01-07T05:31:04","slug":"stored-procedure-to-insert-data-to-parent-and-child-using-xml","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1318","title":{"rendered":"Stored Procedure to insert data to parent and child using XML"},"content":{"rendered":"<p>Couple of days back I wrote an <a href=\"https:\/\/techsatwork.com\/blog\/?p=1313\" target=\"_blank\">article<\/a> that showed how to quickly insert a child table with a \u00c2\u00a0parent table having generated column. On this one I am going to show you how to insert data using XML as a value. \u00c2\u00a0As 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) :<\/p>\n<p><span style=\"color: #ff9900;\">create table orders<\/span><br \/>\n<span style=\"color: #ff9900;\">(order_id\u00c2\u00a0 int not null<\/span><br \/>\n<span style=\"color: #ff9900;\">generated always as identity (start with 1000,increment by 1, nocache)<\/span><br \/>\n<span style=\"color: #ff9900;\">,customer_id int not null<\/span><br \/>\n<span style=\"color: #ff9900;\">,order_timestamp\u00c2\u00a0 timestamp<\/span><br \/>\n<span style=\"color: #ff9900;\">,order_channel_id\u00c2\u00a0 varchar(5)<\/span><br \/>\n<span style=\"color: #ff9900;\">,csr_id\u00c2\u00a0 varchar(10)<\/span><br \/>\n<span style=\"color: #ff9900;\">,primary key (order_id)<\/span><br \/>\n<span style=\"color: #ff9900;\">);<\/span><br \/>\n<span style=\"color: #ff9900;\">create table order_items<\/span><br \/>\n<span style=\"color: #ff9900;\">(order_id\u00c2\u00a0 int not null<\/span><br \/>\n<span style=\"color: #ff9900;\">,seq_num\u00c2\u00a0 int not null<\/span><br \/>\n<span style=\"color: #ff9900;\">,product_id\u00c2\u00a0 varchar(20) not null<\/span><br \/>\n<span style=\"color: #ff9900;\">,qty int<\/span><br \/>\n<span style=\"color: #ff9900;\">,unit_price dec(9,2)<\/span><br \/>\n<span style=\"color: #ff9900;\">,product_discount dec(9,2)<\/span><br \/>\n<span style=\"color: #ff9900;\">,primary key(order_id,seq_num)<\/span><br \/>\n<span style=\"color: #ff9900;\">);<\/span><\/p>\n<p>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\u00c2\u00a0demonstrating\u00c2\u00a0a method.<\/p>\n<p><span style=\"color: #ff9900;\">create or replace procedure spIorders<\/span><br \/>\n<span style=\"color: #ff9900;\">(in customerid int<\/span><br \/>\n<span style=\"color: #ff9900;\">,in orderchannelid varchar(5)<\/span><br \/>\n<span style=\"color: #ff9900;\">,in csrid varchar(10)<\/span><br \/>\n<span style=\"color: #ff9900;\">,in orderitems XML<\/span><br \/>\n<span style=\"color: #ff9900;\">,out orderid int<\/span><br \/>\n<span style=\"color: #ff9900;\">)<\/span><br \/>\n<span style=\"color: #ff9900;\">LANGUAGE SQL<\/span><br \/>\n<span style=\"color: #ff9900;\">NO EXTERNAL ACTION<\/span><br \/>\n<span style=\"color: #ff9900;\">P1:BEGIN<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8212; declaring global temporary table to hold the data<\/span><br \/>\n<span style=\"color: #ff9900;\">declare global temporary table session.gtt_orders<\/span><br \/>\n<span style=\"color: #ff9900;\">(customer_id int not null<\/span><br \/>\n<span style=\"color: #ff9900;\">,order_channel_id varchar(5)<\/span><br \/>\n<span style=\"color: #ff9900;\">,csr_id varchar(10)<\/span><br \/>\n<span style=\"color: #ff9900;\">,order_item XML)<\/span><br \/>\n<span style=\"color: #ff9900;\">on commit delete rows<\/span><br \/>\n<span style=\"color: #ff9900;\">not logged;<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8212; insert the data to the global temporary table from the data passed in<\/span><br \/>\n<span style=\"color: #ff9900;\">insert into session.gtt_orders<\/span><br \/>\n<span style=\"color: #ff9900;\">(customer_id,order_channel_id,csr_id,order_item)<\/span><br \/>\n<span style=\"color: #ff9900;\">values<\/span><br \/>\n<span style=\"color: #ff9900;\">(customerid<\/span><br \/>\n<span style=\"color: #ff9900;\">,orderchannelid<\/span><br \/>\n<span style=\"color: #ff9900;\">,csrid<\/span><br \/>\n<span style=\"color: #ff9900;\">,orderitems);<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8211;insert into parent table (orders) from global temporary table<\/span><br \/>\n<span style=\"color: #ff9900;\">select order_id into orderid<\/span><br \/>\n<span style=\"color: #ff9900;\">from new table<\/span><br \/>\n<span style=\"color: #ff9900;\">(insert into orders<\/span><br \/>\n<span style=\"color: #ff9900;\">(customer_id,order_timestamp,order_channel_id,csr_id)<\/span><br \/>\n<span style=\"color: #ff9900;\">select gtt.customer_id,current_timestamp,gtt.order_channel_id,gtt.csr_id<\/span><br \/>\n<span style=\"color: #ff9900;\">from session.gtt_orders gtt);<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8211;insert into child table (order_items) from global temporary table<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8211;xmltable will split the XML document to multiple rows<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8211;row_number() over() is an olap function that will generate rows number for seq_num<\/span><br \/>\n<span style=\"color: #ff9900;\">insert into order_items<\/span><br \/>\n<span style=\"color: #ff9900;\">(order_id,seq_num,product_id,qty,unit_price,product_discount)<\/span><br \/>\n<span style=\"color: #ff9900;\">select orderid,row_number() over(),x.productid,x.qty,x.unitprice,x.productdiscount<\/span><br \/>\n<span style=\"color: #ff9900;\">from session.gtt_orders gtt,<\/span><br \/>\n<span style=\"color: #ff9900;\">xmltable(&#8216;$c\/orderitem\/item&#8217; passing gtt.order_item as &#8220;c&#8221;<\/span><br \/>\n<span style=\"color: #ff9900;\">columns<\/span><br \/>\n<span style=\"color: #ff9900;\">productid varchar(20) path &#8216;productid&#8217;,<\/span><br \/>\n<span style=\"color: #ff9900;\">qty int path &#8216;qty&#8217;,<\/span><br \/>\n<span style=\"color: #ff9900;\">unitprice dec(9,2) path &#8216;unitprice&#8217;,<\/span><br \/>\n<span style=\"color: #ff9900;\">productdiscount dec(9,2) path &#8216;productdiscount&#8217;) as x;<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8212;<\/span><br \/>\n<span style=\"color: #ff9900;\">commit;<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8212; dropping the global temporary table since the inserts have be done.<\/span><br \/>\n<span style=\"color: #ff9900;\">drop table session.gtt_orders;<\/span><br \/>\n<span style=\"color: #ff9900;\">END P1<\/span><br \/>\n<span style=\"color: #ff9900;\">@<\/span><\/p>\n<p>Now lets call the stored procedure we just created an pass in an XML that has three order items :<\/p>\n<p><span style=\"color: #ff9900;\">call spIorders (5000,&#8217;Web&#8217;,&#8217;RAJU&#8217;,<\/span><br \/>\n<span style=\"color: #ff9900;\">xmlparse(document<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8216;&lt;orderitem&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;item&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;productid&gt;iPad3-Cell-64&lt;\/productid&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;qty&gt;10&lt;\/qty&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;unitprice&gt;829.00&lt;\/unitprice&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;productdiscount&gt;50.00&lt;\/productdiscount&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;\/item&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;item&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;productid&gt;iPad2-Wifi-16&lt;\/productid&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;qty&gt;5&lt;\/qty&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;unitprice&gt;629.00&lt;\/unitprice&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;productdiscount&gt;0.00&lt;\/productdiscount&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;\/item&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;item&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;productid&gt;iPod-Tch-8&lt;\/productid&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;qty&gt;100&lt;\/qty&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;unitprice&gt;199.00&lt;\/unitprice&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;productdiscount&gt;100.00&lt;\/productdiscount&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;\/item&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&lt;\/orderitem&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\">&#8216; preserve whitespace),?);<\/span><\/p>\n<p>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.<br \/>\nPlease feel free to comment on this and possible improvements or alternate methods.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Couple of days back I wrote an article that showed how to quickly insert a child table with a \u00c2\u00a0parent table having generated column. On this one I am going to show you how to insert data using XML as a value. \u00c2\u00a0As my previous article show its easy to insert to child table , [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[3,358],"tags":[799,802,808,809,805,804,800,746,803,801,807,806],"class_list":["post-1318","post","type-post","status-publish","format-standard","hentry","category-db2","category-how-to","tag-db2-stored-procedure","tag-db2-xml","tag-inserting-child-data-using-xml","tag-inserting-multiple-rows-using-xml","tag-inserting-xml-data","tag-inserting-xml-to-db2-table","tag-purexml","tag-stored-procedure","tag-xml-column-type","tag-xml-query","tag-xmlparse","tag-xmltable"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1318"}],"version-history":[{"count":4,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1318\/revisions"}],"predecessor-version":[{"id":1320,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1318\/revisions\/1320"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}