{"id":203,"date":"2007-11-21T20:27:36","date_gmt":"2007-11-21T19:27:36","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=203"},"modified":"2007-11-21T20:27:36","modified_gmt":"2007-11-21T19:27:36","slug":"oracle-11g-virtual-columns","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=203","title":{"rendered":"Oracle 11G &#8211; Virtual Columns"},"content":{"rendered":"<p>As written <a title=\"Oracle 11G - Follows in Trigger\" href=\"http:\/\/bar-solutions.com\/wordpress\/?p=201\" target=\"_blank\">before<\/a>, I am currently trying out some things on Oracle 11G after a presentation by Lucas and Alex of <a title=\"AMIS - Home\" href=\"http:\/\/www.amis.nl\" target=\"_blank\">Amis<\/a>. One of the new features they talked about was the virtual columns option in Oracle. This a column based on an expression that doesn&#8217;t really exist in the database.<\/p>\n<p><!--more--><\/p>\n<p>Here&#8217;s how to create a virtual column:<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:d120e700-ac11-4e54-a4ae-c9ac4788aa70\" 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: #000000; \">column [datatype] [GENERATED ALWAYS] AS <\/span><span style=\"color: #000000; \">(<\/span><span style=\"color: #000000; \">column_expression<\/span><span style=\"color: #000000; \">)<\/span><span style=\"color: #000000; \">\n   [VIRTUAL]\n   [ inline_constraint [inline_constraint]<\/span><span style=\"color: #000000; \">...<\/span><span style=\"color: #000000; \"> ]<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>for example<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ad933bbc-2092-409c-8d39-4ac908b90786\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; width: 587px; padding-top: 0px\">\n<pre style=\"background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: auto;\"><div><!--\n\nCode highlighting produced by Actipro CodeHighlighter (freeware)\nhttp:\/\/www.CodeHighlighter.com\/\n\n--><span style=\"color: #0000FF; \">ALTER<\/span><span style=\"color: #000000; \"> <\/span><span style=\"color: #0000FF; \">TABLE<\/span><span style=\"color: #000000; \"> T_VIRTUALCOLUMNTEST <\/span><span style=\"color: #0000FF; \">ADD<\/span><span style=\"color: #000000; \"> (income_1 <\/span><span style=\"color: #0000FF; \">AS<\/span><span style=\"color: #000000; \"> (salary <\/span><span style=\"color: #808080; \">+<\/span><span style=\"color: #000000; \"> nvl(commission_1,<\/span><span style=\"color: #800000; font-weight: bold; \">0<\/span><span style=\"color: #000000; \">)));<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>Of course the same behavior can be achieved using real&nbsp;(normal) columns and triggers, but this&nbsp;approach requires less coding. Of course I tried to create a virtual column based on another virtual column because I wanted to achieve some sort of Single Point of Definition, but that was not possible. I got an error telling me this is not possible.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:4312653c-a334-426b-a67c-45aca1d90eef\" 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: #000000; \">ORA-<\/span><span style=\"color: #000000; \">54012<\/span><span style=\"color: #000000; \">: virtual column is referenced in a column expression<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>This can&nbsp;however be resolved by adding a real column which is updated by a trigger. This trigger will be invalidated&nbsp;when the&nbsp;virtual column&nbsp;is dropped from the table, but the value is preserved in the real column. The trigger will be invalid though.<\/p>\n<p>An <a title=\"ORACLE-BASE - Virtual Columns in Oracle Database 11g Release 1\" href=\"http:\/\/www.oracle-base.com\/articles\/11g\/VirtualColumns_11gR1.php\" target=\"_blank\">article<\/a> on <a title=\"ORACLE-BASE - Oracle DBA and development articles, scripts, HOWTOs and forums (8i, 9i, 10g, 11g)\" href=\"http:\/\/www.oracle-base.com\/\" target=\"_blank\">Oracle Base<\/a> wrote down the follow cotcha&#8217;s (amongst others):<\/p>\n<ul>\n<li>The expression used in the virtual column definition has the following restrictions:\n<ul>\n<li>It cannot refer to another virtual column by name.\n<\/li>\n<li>It can only refer to columns defined in the same table.\n<\/li>\n<li>If it refers to a deterministic user-defined function, it cannot be used as a partitioning key column.\n<\/li>\n<li>The output of the expression must be a scalar value. It cannot return an Oracle supplied datatype, a user-defined type, or LOB or LONG RAW. <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>So, I thought, how about using a function&nbsp;for the virtual column.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:02a39bb1-8f15-4bc9-8e77-03979cfdbf09\" 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: #000000; \">create or replace function sf_total\n  ( value1_in <\/span><span style=\"color: #0000FF; \">in<\/span><span style=\"color: #000000; \"> NUMBER\n  , value2_in <\/span><span style=\"color: #0000FF; \">in<\/span><span style=\"color: #000000; \"> number)\n<\/span><span style=\"color: #0000FF; \">return<\/span><span style=\"color: #000000; \"> number \ndeterministic\n<\/span><span style=\"color: #0000FF; \">is<\/span><span style=\"color: #000000; \">\n  l_returnvalue number;\nbegin\n  l_returnvalue :<\/span><span style=\"color: #000000; \">=<\/span><span style=\"color: #000000; \"> value1_in <\/span><span style=\"color: #000000; \">+<\/span><span style=\"color: #000000; \"> value2_in;\n  <\/span><span style=\"color: #0000FF; \">return<\/span><span style=\"color: #000000; \">(l_returnvalue);\nend sf_total;<\/span><\/div><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>But when I tried this, using two virtual columns as parameters I got the&nbsp;same error as mentioned earlier. I guess this is covered by the first restriction. Too bad.&nbsp;<\/p>\n<p>Some useful links about virtual columns:<\/p>\n<p><a title=\"CREATE TABLE\" href=\"http:\/\/download.oracle.com\/docs\/cd\/B28359_01\/server.111\/b28286\/statements_7002.htm\" target=\"_blank\">CREATE TABLE Documentation<\/a><br \/><a title=\"ORACLE-BASE - Virtual Columns in Oracle Database 11g Release 1\" href=\"http:\/\/www.oracle-base.com\/articles\/11g\/VirtualColumns_11gR1.php\" target=\"_blank\">Virtual Columns in Oracle Database 11g Release 1<\/a><br \/><a title=\"Understanding Virtual Columns\" href=\"http:\/\/www.oraclebrains.com\/?p=137\" target=\"_blank\">Understanding Virtual Columns<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As written before, I am currently trying out some things on Oracle 11G after a presentation by Lucas and Alex of Amis. One of the new features they talked about was the virtual columns option in Oracle. This a column based on an expression that doesn&#8217;t really exist in the database.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5,9],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql","category-sql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/203","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=203"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}