{"id":220,"date":"2008-02-07T19:37:49","date_gmt":"2008-02-07T18:37:49","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=220"},"modified":"2008-02-15T13:53:35","modified_gmt":"2008-02-15T12:53:35","slug":"blogging-block","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=220","title":{"rendered":"Blogging block&#8230;"},"content":{"rendered":"<p>I seem to be suffering from a blogging block (like a writers block, but for a blogger). Maybe it&#8217;s my new job at <a title=\"AMIS Technology blog\" href=\"http:\/\/technology.amis.nl\/blog\/\" target=\"_blank\">AMIS<\/a> or the new application at the KPN I am working on. It&#8217;s a lot of figuring out what is going on. Reading through other peoples code and try to understand their mind setting and the path they followed.<\/p>\n<p>It did however teach me new usage of SQL, for instance the <a title=\"WITH Clause\" href=\"http:\/\/bar-solutions.com\/wordpress\/wp-admin\/post.php?action=edit&amp;post=219\" target=\"_blank\" rel=\"nofollow\">WITH clause<\/a> which can be used instead of inline views and I recently came across a use of the update statement I didn&#8217;t know of.     <\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:df468288-c174-4b5b-b655-402710669671\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\r\n\r\nCode highlighting produced by Actipro CodeHighlighter (freeware)\r\nhttp:\/\/www.CodeHighlighter.com\/\r\n\r\n--><span style=\"color: #0000FF; \">UPDATE<\/span><span style=\"color: #000000; \"> emp a\r\n   <\/span><span style=\"color: #0000FF; \">SET<\/span><span style=\"color: #000000; \"> ( sal, comm ) <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \"> ( <\/span><span style=\"color: #0000FF; \">SELECT<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">1.5<\/span><span style=\"color: #000000; \"> newsal\r\n                              , NVL( comm, <\/span><span style=\"color: #800000; font-weight: bold; \">0<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #808080; \">+<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">100<\/span><span style=\"color: #000000; \"> newcomm\r\n                          <\/span><span style=\"color: #0000FF; \">FROM<\/span><span style=\"color: #000000; \"> emp b\r\n                         <\/span><span style=\"color: #0000FF; \">WHERE<\/span><span style=\"color: #000000; \"> a.empno <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \"> b.empno );\r\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p><!--more--><\/p>\n<p>I was used to update statements like this:<br \/>\n  <\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e5c7699c-eb50-482c-a88f-854a10d25105\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\r\n\r\nCode highlighting produced by Actipro CodeHighlighter (freeware)\r\nhttp:\/\/www.CodeHighlighter.com\/\r\n\r\n--><span style=\"color: #0000FF; \">update<\/span><span style=\"color: #000000; \"> emp a\r\n   <\/span><span style=\"color: #0000FF; \">set<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">1.5<\/span><span style=\"color: #000000; \">;\r\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>And if you wanted to update two columns, just add another update statement:<br \/>\n  <\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:dea2549e-8bd1-4f94-a778-453ac6127fb4\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\r\n\r\nCode highlighting produced by Actipro CodeHighlighter (freeware)\r\nhttp:\/\/www.CodeHighlighter.com\/\r\n\r\n--><span style=\"color: #0000FF; \">update<\/span><span style=\"color: #000000; \"> emp a\r\n   <\/span><span style=\"color: #0000FF; \">set<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #808080; \">=<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">1.5<\/span><span style=\"color: #000000; \">;\r\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>But, if you would want to update a lot of columns this would involve a lot of statements, hence a lot of round trips to the database. Since I am a PL\/SQL guy, I think I would have written a small loop to accomplish the same result:<br \/>\n  <\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:25d34df9-27f6-470c-b6a0-c3567c91eab4\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\r\n\r\nCode highlighting produced by Actipro CodeHighlighter (freeware)\r\nhttp:\/\/www.CodeHighlighter.com\/\r\n\r\n--><span style=\"color: #0000FF; \">begin<\/span><span style=\"color: #000000; \">\r\n  <\/span><span style=\"color: #0000FF; \">for<\/span><span style=\"color: #000000; \"> rec <\/span><span style=\"color: #0000FF; \">in<\/span><span style=\"color: #000000; \"> (<\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> empno\r\n                   , sal <\/span><span style=\"color: #000000; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800080; \">1.5<\/span><span style=\"color: #000000; \"> newsal\r\n                   , nvl (comm, <\/span><span style=\"color: #800080; \">0<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #000000; \">+<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800080; \">100<\/span><span style=\"color: #000000; \"> newcomm\r\n                <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> emp) <\/span><span style=\"color: #0000FF; \">loop<\/span><span style=\"color: #000000; \">\r\n    <\/span><span style=\"color: #0000FF; \">update<\/span><span style=\"color: #000000; \"> emp\r\n       <\/span><span style=\"color: #0000FF; \">set<\/span><span style=\"color: #000000; \"> emp.sal <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> rec.newsal\r\n         , emp.comm <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> rec.newcomm\r\n     <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> emp.empno <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> rec.empno;\r\n  <\/span><span style=\"color: #0000FF; \">end<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">loop<\/span><span style=\"color: #000000; \">;\r\n<\/span><span style=\"color: #0000FF; \">end<\/span><span style=\"color: #000000; \">;\r\n<\/span><span style=\"color: #000000; \">\/<\/span><span style=\"color: #000000; \">\r\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>This would require even more round trips to the database, but it&#8217;s a bit more readable (I think). Maybe not for just two columns, but when you have more columns, it might be more readable.<br \/>\n  <br \/>To speed things up a bit, I would use bulk operations to minimize the round trips to and from the database: <\/p>\n<p><\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a66df8f3-8761-4545-85a0-992c9ff4ba0f\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\r\n\r\nCode highlighting produced by Actipro CodeHighlighter (freeware)\r\nhttp:\/\/www.CodeHighlighter.com\/\r\n\r\n--><span style=\"color: #0000FF; \">declare<\/span><span style=\"color: #000000; \">\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> type declarations<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">type<\/span><span style=\"color: #000000; \"> sal_tp <\/span><span style=\"color: #0000FF; \">is<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">table<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">of<\/span><span style=\"color: #000000; \"> emp.sal%<\/span><span style=\"color: #0000FF; \">type<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">index<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">by<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">binary_integer<\/span><span style=\"color: #000000; \">;\r\n  <\/span><span style=\"color: #0000FF; \">type<\/span><span style=\"color: #000000; \"> comm_tp <\/span><span style=\"color: #0000FF; \">is<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">table<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">of<\/span><span style=\"color: #000000; \"> emp.comm%<\/span><span style=\"color: #0000FF; \">type<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">index<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">by<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">binary_integer<\/span><span style=\"color: #000000; \">;\r\n  <\/span><span style=\"color: #0000FF; \">type<\/span><span style=\"color: #000000; \"> empno_tp <\/span><span style=\"color: #0000FF; \">is<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">table<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">of<\/span><span style=\"color: #000000; \"> emp.empno%<\/span><span style=\"color: #0000FF; \">type<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">index<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">by<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">binary_integer<\/span><span style=\"color: #000000; \">;\r\n\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> variable based on previously declared types<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  empsal sal_tp;\r\n  empcomm comm_tp;\r\n  empempno empno_tp;\r\n<\/span><span style=\"color: #0000FF; \">begin<\/span><span style=\"color: #000000; \">\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> select the values using bulk collect to get them into the local variables<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">select<\/span><span style=\"color: #000000; \"> empno\r\n       , sal <\/span><span style=\"color: #000000; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800080; \">1.5<\/span><span style=\"color: #000000; \"> newsal\r\n       , nvl (comm, <\/span><span style=\"color: #800080; \">0<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #000000; \">+<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800080; \">100<\/span><span style=\"color: #000000; \"> newcomm\r\n   <\/span><span style=\"color: #0000FF; \">bulk<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">collect<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">into<\/span><span style=\"color: #000000; \"> empempno, empsal, empcomm\r\n   <\/span><span style=\"color: #0000FF; \">from<\/span><span style=\"color: #000000; \"> emp;\r\n\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> use bulk update statement to update the database<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">forall<\/span><span style=\"color: #000000; \"> i <\/span><span style=\"color: #0000FF; \">in<\/span><span style=\"color: #000000; \"> empempno.first .. empempno.last\r\n    <\/span><span style=\"color: #0000FF; \">update<\/span><span style=\"color: #000000; \"> emp\r\n       <\/span><span style=\"color: #0000FF; \">set<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> empsal(i)\r\n         , comm <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> empcomm(i)\r\n     <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> empno <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> empempno(i);\r\n<\/span><span style=\"color: #0000FF; \">end<\/span><span style=\"color: #000000; \">;\r\n<\/span><span style=\"color: #000000; \">\/<\/span><span style=\"color: #000000; \">\r\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>But, for every column I&#8217;d use, I would need to create a type and a variable. If I were to use the QDA on my tables, the code would look something like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5c3b441e-b136-48ba-b119-927305bde09f\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<pre style=\"background-color:White;;overflow: auto;\"><div><!--\r\n\r\nCode highlighting produced by Actipro CodeHighlighter (freeware)\r\nhttp:\/\/www.CodeHighlighter.com\/\r\n\r\n--><span style=\"color: #0000FF; \">declare<\/span><span style=\"color: #000000; \">\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> table collection<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  emps emp_tp.emp_tc;\r\n\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> column collections<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  empsal emp_tp.SAL_cc;\r\n  empcomm emp_tp.COMM_cc;\r\n  empempno emp_tp.EMPNO_cc;\r\n<\/span><span style=\"color: #0000FF; \">begin<\/span><span style=\"color: #000000; \">\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> get all the values from the table in a table collection<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  emps :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> emp_qp.allrows;\r\n\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> move the individual columns to their own column collection<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">for<\/span><span style=\"color: #000000; \"> i <\/span><span style=\"color: #0000FF; \">in<\/span><span style=\"color: #000000; \"> emps.first .. emps.last <\/span><span style=\"color: #0000FF; \">loop<\/span><span style=\"color: #000000; \">\r\n    empsal(i) :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> emps(i).sal <\/span><span style=\"color: #000000; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800080; \">1.5<\/span><span style=\"color: #000000; \">;\r\n    empcomm(i) :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> nvl(emps(i).comm, <\/span><span style=\"color: #800080; \">0<\/span><span style=\"color: #000000; \">) <\/span><span style=\"color: #000000; \">+<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800080; \">100<\/span><span style=\"color: #000000; \">;\r\n    empempno(i) :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> emps(i).empno;\r\n  <\/span><span style=\"color: #0000FF; \">end<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">loop<\/span><span style=\"color: #000000; \">;\r\n\r\n  <\/span><span style=\"color: #008000; \">--<\/span><span style=\"color: #008000; \"> use bulk update statement to update the database<\/span><span style=\"color: #008000; \">\r\n<\/span><span style=\"color: #000000; \">  <\/span><span style=\"color: #0000FF; \">forall<\/span><span style=\"color: #000000; \"> i <\/span><span style=\"color: #0000FF; \">in<\/span><span style=\"color: #000000; \"> empempno.first .. empempno.last\r\n    <\/span><span style=\"color: #0000FF; \">update<\/span><span style=\"color: #000000; \"> emp\r\n       <\/span><span style=\"color: #0000FF; \">set<\/span><span style=\"color: #000000; \"> sal <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> empsal(i)\r\n         , comm <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> empcomm(i)\r\n     <\/span><span style=\"color: #0000FF; \">where<\/span><span style=\"color: #000000; \"> empno <\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> empempno(i);\r\n<\/span><span style=\"color: #0000FF; \">end<\/span><span style=\"color: #000000; \">;\r\n<\/span><span style=\"color: #000000; \">\/<\/span><span style=\"color: #000000; \">\r\n<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<\/p>\n<p>All of these examples result in the same outcome, but one takes more time than the other. Depending on the calculations that need to done on the columns you might choose to do this is SQL or in PLSQL.&#160; I think most of the real world issues can be solved by just using the SQL approach, but as you can see, there are more ways to solve a problem.<\/p>\n<p>Hey, it seems that I didn&#8217;t have a real bloggers block after all. \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I seem to be suffering from a blogging block (like a writers block, but for a blogger). Maybe it&#8217;s my new job at AMIS or the new application at the KPN I am working on. It&#8217;s a lot of figuring out what is going on. Reading through other peoples code and try to understand their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,5,9],"tags":[],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-codegen","category-plsql","category-sql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}