{"id":187,"date":"2007-09-09T15:05:35","date_gmt":"2007-09-09T14:05:35","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=187"},"modified":"2007-09-09T15:05:35","modified_gmt":"2007-09-09T14:05:35","slug":"using-inline-views-to-achieve-spod","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=187","title":{"rendered":"Using inline views to achieve SPOD"},"content":{"rendered":"<p>If you are wondering what SPOD is, then check out this <a title=\"Toad World &gt; Community &gt; Experts Blog - Three Tips Most Excellent for PL\/SQL Developers\" href=\"http:\/\/www.toadworld.com\/Community\/ExpertsBlog\/tabid\/67\/EntryID\/31\/Default.aspx\" target=\"_blank\">blog post<\/a> by <a title=\"The Cyberworld of Steven Feuerstein\" href=\"http:\/\/www.stevenfeuerstein.com\/\" target=\"_blank\">Steven Feuerstein<\/a>. Suppose you want to create a&nbsp;SQL statement (for a view or cursor&nbsp;or something else) where you want the value of one mathematical function&nbsp;to be used as input for another mathematical function. For simplicity reasons I am just using some simple numbers, but you can make this as complex as you want.<\/p>\n<p><!--more--><\/p>\n<p>Of course you can easily add basically the same function over and over again in the statement, like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e09ee820-c31a-48ff-a373-3a744090f98b\" contenteditable=\"false\" 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;;font-family:Microsoft Sans Serif;font-size:9.75\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:09824c02-95b6-4fe2-9a04-222f493530a2\" contenteditable=\"false\" 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><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">SELECT<\/span><span style=\"color: #000000; \"> ( <\/span><span style=\"color: #800000; font-weight: bold; \">2<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">3<\/span><span style=\"color: #000000; \"> ) a\n     , ( ( <\/span><span style=\"color: #800000; font-weight: bold; \">2<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">3<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">4<\/span><span style=\"color: #000000; \"> ) b\n     , ( ( <\/span><span style=\"color: #800000; font-weight: bold; \">2<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">3<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">5<\/span><span style=\"color: #000000; \"> ) c\n     , ( ( ( <\/span><span style=\"color: #800000; font-weight: bold; \">2<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">3<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">5<\/span><span style=\"color: #000000; \"> ) <\/span><span style=\"color: #808080; \">+<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">10<\/span><span style=\"color: #000000; \"> ) d\n  <\/span><span style=\"color: #0000FF; \">FROM<\/span><span style=\"color: #000000; \"> DUAL;<\/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, what if the formula for&nbsp;<em>c<\/em> changes. That would mean changing both the formula for <em>c<\/em> <u>and<\/u> for <em>d<\/em>. Or even worse, what if the formula for <em>a<\/em> changes. That would mean changing all the formulas for <em>a<\/em>, <em>b<\/em>, <em>c<\/em> and <em>d<\/em>.<\/p>\n<p>Not really SPOD (if you don&#8217;t know what it is by now, it <u>S<\/u>ingle <u>P<\/u>oint <u>O<\/u>f <u>D<\/u>efinition). We really want to enter each formula just once and then be able to use it results in another formula. This is where the inline views come into play. Remember that inline views are noting more than&nbsp;SQL statements in the from clause. It&#8217;s like creating a view at runtime, without it being physically created in the database.<\/p>\n<p>If you&nbsp;want to select&nbsp;something from a table, this table has to be in the from clause. It&#8217;s the same with&nbsp;inline views. If you want to select anything from a &#8216;lower level&#8217;, be sure to include it in the select of the inline view. I call it, bubbling up values (like the bubbles coming to the surface in your favorite soft-drink).<\/p>\n<p>Consider the following code: <\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:c3d4c2da-fb2d-4fea-a404-eeee5419743f\" contenteditable=\"false\" 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><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">SELECT<\/span><span style=\"color: #000000; \"> a\n     , b\n     , c\n     , c <\/span><span style=\"color: #808080; \">+<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">10<\/span><span style=\"color: #000000; \"> d\n  <\/span><span style=\"color: #0000FF; \">FROM<\/span><span style=\"color: #000000; \"> ( <\/span><span style=\"color: #0000FF; \">SELECT<\/span><span style=\"color: #000000; \"> a <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">4<\/span><span style=\"color: #000000; \"> b\n              , a <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">5<\/span><span style=\"color: #000000; \"> c\n              , a\n          <\/span><span style=\"color: #0000FF; \">FROM<\/span><span style=\"color: #000000; \"> ( <\/span><span style=\"color: #0000FF; \">SELECT<\/span><span style=\"color: #000000; \"> ( <\/span><span style=\"color: #800000; font-weight: bold; \">2<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #808080; \">*<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #800000; font-weight: bold; \">3<\/span><span style=\"color: #000000; \"> ) a\n                  <\/span><span style=\"color: #0000FF; \">FROM<\/span><span style=\"color: #000000; \"> DUAL ))<\/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>Looks more complex, but at least we&#8217;ve achieved Single Point Of Definition. Remember the following things:<\/p>\n<ul>\n<li>The formula you will be using as input for other formulas must be in the deepest level.\n<\/li>\n<li>Everything you want to be able to select on a higher level, must be selected on the level directly below that one.\n<\/li>\n<li>Use aliases to be able to reference the result in a higher level.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you are wondering what SPOD is, then check out this blog post by Steven Feuerstein. Suppose you want to create a&nbsp;SQL statement (for a view or cursor&nbsp;or something else) where you want the value of one mathematical function&nbsp;to be used as input for another mathematical function. For simplicity reasons I am just using some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,9],"tags":[],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-oracle","category-sql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/187","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=187"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/187\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}