{"id":1313,"date":"2012-05-24T03:54:16","date_gmt":"2012-05-23T21:54:16","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1313"},"modified":"2016-01-07T11:31:04","modified_gmt":"2016-01-07T05:31:04","slug":"inserting-child-table-with-parent-table-having-generated-column","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1313","title":{"rendered":"Inserting child table with parent table having generated column"},"content":{"rendered":"<p>I am sure we all have come across situations were we have to populate a child table with a value that has been generated in a parent table. If you don&#8217;t know how to handle it can take a bit of coding. Recently I came across a situation at work where a newbie was trying to populate a child table with an id that was generated at the parent table.<br \/>\nThe girl first inserted the data to the parent table which has an id column defined as GENERATED ALWAYS. She then selected the row to retrieve the generated value and then used it to populate the child table.<\/p>\n<p>Whats wrong with this ? Nothing wrong with it unless you don&#8217;t want to kill the I\/O system on a heavily used table. \u00c2\u00a0So I introduced her to the famous \u00c2\u00a0select from insert statement. Yes you heard it right&#8230;select from insert. \u00c2\u00a0Surely everybody knows \u00c2\u00a0INSERT SELECT, but \u00c2\u00a0SELECT from INSERT is a slick feature. \u00c2\u00a0You can retrieve the data you inserted without doing any additional I\/O. \u00c2\u00a0Here is a small example :<\/p>\n<p>Lets create two sample tables :<\/p>\n<p><span style=\"color: #99cc00;\">create table orders<\/span><br \/>\n<span style=\"color: #99cc00;\"> (order_id\u00c2\u00a0 int not null<\/span><br \/>\n<span style=\"color: #99cc00;\"> generated always as identity (start with 1000,increment by 1, nocache)<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,customer_id int not null<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,order_timestamp\u00c2\u00a0 timestamp<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,order_channel_id\u00c2\u00a0 varchar(5)<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,csr_id\u00c2\u00a0 varchar(10)<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,primary key (order_id)<\/span><br \/>\n<span style=\"color: #99cc00;\"> );<\/span><br \/>\n<span style=\"color: #99cc00;\"> create table order_items<\/span><br \/>\n<span style=\"color: #99cc00;\"> (order_id\u00c2\u00a0 int not null<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,seq_num\u00c2\u00a0 int not null<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,product_id\u00c2\u00a0 varchar(20) not null<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,qty int<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,unit_price dec(9,2)<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,product_discount dec(9,2)<\/span><br \/>\n<span style=\"color: #99cc00;\"> ,primary key(order_id,seq_num)<\/span><br \/>\n<span style=\"color: #99cc00;\"> );<\/span><\/p>\n<p>Now lets try to add a row to orders table and then using the generated order_id we will add a row to the order_items table.<\/p>\n<p><span style=\"color: #99cc00;\">create or replace variable vseq int;<\/span><br \/>\n<span style=\"color: #99cc00;\"> set vseq = 1;<\/span><br \/>\n<span style=\"color: #99cc00;\"> create or replace variable vorder int;<\/span><br \/>\n<span style=\"color: #99cc00;\"> set vorder =<\/span><br \/>\n<span style=\"color: #99cc00;\"> (select order_id<\/span><br \/>\n<span style=\"color: #99cc00;\"> from new table (<\/span><br \/>\n<span style=\"color: #99cc00;\"> insert into orders<\/span><br \/>\n<span style=\"color: #99cc00;\"> (customer_id,order_timestamp,order_channel_id,csr_id)<\/span><br \/>\n<span style=\"color: #99cc00;\"> values(5000,current_timestamp,&#8217;web&#8217;,&#8217;raju&#8217;)));<\/span><\/p>\n<p><span style=\"color: #99cc00;\">insert into order_items<\/span><br \/>\n<span style=\"color: #99cc00;\"> values(vorder,vseq,&#8217;A001&#8242;,10,100.00,0.00);<\/span><\/p>\n<p><span style=\"color: #99cc00;\">set vseq = vseq + 1;<\/span><\/p>\n<p><span style=\"color: #99cc00;\">insert into order_items<\/span><br \/>\n<span style=\"color: #99cc00;\"> values(vorder,vseq,&#8217;B002&#8242;,25,35.50,5.50);<\/span><\/p>\n<p>The reason why I added the vseq variable was to show you that it can also be done. Another thing you can do while you are still connected on the same session you can issue :\u00c2\u00a0<span style=\"color: #99cc00;\">select vorder from sysibm.sysdummy1<\/span>\u00c2\u00a0\u00c2\u00a0 to retrieve the value of the variable.<\/p>\n<p>The variable is dropped when the connection or session is terminated. However its a good practice to drop the variable at the end of the transactions :<\/p>\n<p><span style=\"color: #99cc00;\">DROP VARIABLE vorder;<\/span><\/p>\n<p>You can use the same approach if you are using the sequences<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am sure we all have come across situations were we have to populate a child table with a value that has been generated in a parent table. If you don&#8217;t know how to handle it can take a bit of coding. Recently I came across a situation at work where a newbie was trying [&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":[796,790,794,795,792,798,797,793,791],"class_list":["post-1313","post","type-post","status-publish","format-standard","hentry","category-db2","category-how-to","tag-autogenerated-parent-key","tag-generated-column","tag-insert-child-table-with-generated-value","tag-insert-child-table-with-parent-key","tag-inserting-child-table","tag-retreiving-insert-value","tag-retrieving-generated-value","tag-select-insert","tag-sequence"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1313","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=1313"}],"version-history":[{"count":3,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1313\/revisions"}],"predecessor-version":[{"id":1317,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1313\/revisions\/1317"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}