<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacLochlainns Weblog &#187; Oracle</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/category/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 00:02:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Oracle 11g XE Delay?</title>
		<link>http://blog.mclaughlinsoftware.com/2010/06/28/oracle-11g-xe/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/06/28/oracle-11g-xe/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 19:42:30 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle APEX]]></category>
		<category><![CDATA[Oracle XE]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=4056</guid>
		<description><![CDATA[Somebody posted a comment inquiring about the release of Oracle 11g XE. They felt it had been delayed. As far as I know, the last word on that came from Andy Mendelsohn last October, as published in this InfoWorld article. When Andy Mendelsohn originally announced Oracle 11g XE in an interview with the NY Times, [...]]]></description>
			<content:encoded><![CDATA[<p>Somebody posted a comment inquiring about the release of Oracle 11g XE. They felt it had been delayed. As far as I know, the last word on that came from Andy Mendelsohn last October, as published in this <a href="http://www.infoworld.com/d/applications/oracle-says-no-cost-11g-xpress-edition-still-least-year-away-555">InfoWorld article</a>.</p>
<p>When Andy Mendelsohn originally announced Oracle 11g XE in an interview with the <a href="http://www.nytimes.com/idg/IDG_852573C400693880002574CD000640AD.html">NY Times</a>, he clearly said it would be based on the terminal release of Oracle 11g. I also suspect it&#8217;ll include <a href="http://www.oracle.com/us/corporate/press/081370">APEX 4.0</a>, which was recently released. Maybe we&#8217;ll see the release at or near Oracle Open World 2010.</p>
<p>I don&#8217;t endorse the negative remarks in the InfoWorld article about why Oracle 11g XE isn&#8217;t released. While I have no direct knowledge of the forthcoming release, I think there&#8217;s an alternative explanation for any delay. Based on my involvement with the Oracle 10g XE release, I believe Oracle wants a <em>rock solid</em> starter version. At least, that was a major concern when they released Oracle 10g XE.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/06/28/oracle-11g-xe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Correlated Update Statement</title>
		<link>http://blog.mclaughlinsoftware.com/2010/06/27/correlated-update-statement/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/06/27/correlated-update-statement/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 04:12:49 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle XE]]></category>
		<category><![CDATA[PSM]]></category>
		<category><![CDATA[Persistent Stored Modules]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=4048</guid>
		<description><![CDATA[My students wanted some additional examples on correlated update statements. I&#8217;ve been struggling with what the right way may be to illustrate them. Correlated subqueries are a hard concept to explain to those new to SQL. While correlated update statements seem impossibly obscure to many or inordinately complex. New SQL developers often flee to the [...]]]></description>
			<content:encoded><![CDATA[<p>My students wanted some additional examples on correlated update statements. I&#8217;ve been struggling with what the right way may be to illustrate them.</p>
<p>Correlated subqueries are a hard concept to explain to those new to SQL. While correlated update statements seem impossibly obscure to many or inordinately complex. New SQL developers often flee to the comfort of procedural programs when it comes to update statements.</p>
<p>This uses my video store data model. It&#8217;s a correlated update statement to clean up potential corrupt data. More or less something a DBA might run to ensure a business rule hasn&#8217;t been violated over time. It checks for the correct foreign key value in a table when a dependent table contains one or more than one row of data.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2010/06/CorrelatedUpdateStatementExampleMember.png"><img src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2010/06/CorrelatedUpdateStatementExampleMember.png" alt="" title="Correlated Update Statement Example" border="none" width="583" height="307" class="aligncenter size-full wp-image-4049" /></a></p>
<p>The aqua-green box highlights a subquery that aggregates foreign key columns and groups the result with the foreign key value. The results from this subquery become a run-time view or derived table.  The result set is a foreign key value and a substitute string literal value for each row in the contact table. These results correlate to the update statement&#8217;s rows based on the input parameter. The input parameter is a column from each updated row. </p>
<p>A unique key (or check constraint) exists on the combination of the common_lookup_table, common_lookup_column, and common_lookup_type columns. This ensures that only one row is returned and assigned to the member_type column in the member table. The update statement naturally works in either Oracle or MySQL without any porting changes.</p>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Correlated Update Statement</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
Expand this section to see the clear text for the foregoing image.</p>
<div>
<div style="padding-left:30px" class="panelCode650">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> member m
<span style="color: #993333; font-weight: bold;">SET</span>    member_type <span style="color: #66cc66;">=</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> common_lookup_id
  <span style="color: #993333; font-weight: bold;">FROM</span>   common_lookup
  <span style="color: #993333; font-weight: bold;">WHERE</span>  common_lookup_table <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'MEMBER'</span>
  <span style="color: #993333; font-weight: bold;">AND</span>    common_lookup_column <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'MEMBER_TYPE'</span>           
  <span style="color: #993333; font-weight: bold;">AND</span>    common_lookup_type <span style="color: #66cc66;">=</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>  dt<span style="color: #66cc66;">.</span>member_type
           <span style="color: #993333; font-weight: bold;">FROM</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>   c<span style="color: #66cc66;">.</span>member_id
                   <span style="color: #66cc66;">,</span>        CASE
                              WHEN COUNT<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span> THEN <span style="color: #ff0000;">'GROUP'</span>
                              ELSE <span style="color: #ff0000;">'INDIVIDUAL'</span>
                            END <span style="color: #993333; font-weight: bold;">AS</span> member_type
                   <span style="color: #993333; font-weight: bold;">FROM</span>     contact c
                   <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> c<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span> dt
           <span style="color: #993333; font-weight: bold;">WHERE</span>   dt<span style="color: #66cc66;">.</span>member_id <span style="color: #66cc66;">=</span> m<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

</div>
</div>
</div>
<p style="clear:both">
<p>While this type of solution is powerful in its own right, I thought it might be interesting to see their procedural equivalents. These correlated subqueries run for each row returned by the master query (or outermost statement). Therefore, they act like functions.</p>
<p>Procedural equivalents (or user-defined functions) simplify the update statement like so:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> member m
<span style="color: #993333; font-weight: bold;">SET</span>    member_type <span style="color: #66cc66;">=</span> get_member_type<span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>If you&#8217;re interested in seeing how you would implement this solution in a user-defined function, just expand the dropdown that interest you.</p>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Oracle User-Defined Function (UDF)</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
Expand this section to see how to map this logic to a PL/SQL schema-level function.</p>
<div>
<div style="padding-left:30px" class="panelCode650">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> get_member_type
<span style="color: #66cc66;">&#40;</span>pv_member_id NUMBER<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> NUMBER <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define a collection of strings.</span>
  TYPE type_options <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare local variables.</span>
  lv_dependent NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
  lv_return_id NUMBER;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare a local variable of the collection.</span>
  lv_member_type TYPE_OPTIONS :<span style="color: #66cc66;">=</span> type_options<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'INDIVIDUAL'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'GROUP'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define a dynamic cursor to count the number of foreign key values. </span>
  CURSOR count_contact <span style="color: #66cc66;">&#40;</span>cv_member_id NUMBER<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span>
    <span style="color: #993333; font-weight: bold;">FROM</span>   contact c
    <span style="color: #993333; font-weight: bold;">WHERE</span>  c<span style="color: #66cc66;">.</span>member_id <span style="color: #66cc66;">=</span> cv_member_id;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define a dynamic cursor to find a key for an individual or group member type.    </span>
  CURSOR get_lookup_id <span style="color: #66cc66;">&#40;</span>cv_type VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> common_lookup_id
    <span style="color: #993333; font-weight: bold;">FROM</span>   common_lookup
    <span style="color: #993333; font-weight: bold;">WHERE</span>  common_lookup_context <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'MEMBER'</span>
    <span style="color: #993333; font-weight: bold;">AND</span>    common_lookup_type <span style="color: #66cc66;">=</span> cv_type;
&nbsp;
BEGIN
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Get the number of foreign key values for a contact.</span>
  OPEN  count_contact<span style="color: #66cc66;">&#40;</span>pv_member_id<span style="color: #66cc66;">&#41;</span>;
  FETCH count_contact
  <span style="color: #993333; font-weight: bold;">INTO</span>  lv_dependent;
  CLOSE count_contact;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Open the dynamic cursor with the required value.  </span>
  <span style="color: #993333; font-weight: bold;">IF</span> lv_dependent <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> THEN
    OPEN get_lookup_id<span style="color: #66cc66;">&#40;</span>lv_member_type<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  ELSE
    OPEN get_lookup_id<span style="color: #66cc66;">&#40;</span>lv_member_type<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Get the correct surrogate primary key value.</span>
  FETCH get_lookup_id
  <span style="color: #993333; font-weight: bold;">INTO</span>  lv_return_id;
  CLOSE get_lookup_id;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Return the correct primary key for use as a foreign key.</span>
  <span style="color: #993333; font-weight: bold;">RETURN</span> lv_return_id;
END;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

</div>
</div>
</div>
<p style="clear:both">
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">MySQL User-Defined Function (UDF)</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
Expand this section to see how to map this logic to a Persistent Stored Module (PSM) function.</p>
<div>
<div style="padding-left:30px" class="panelCode650">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'DROP FUNCTION IF EXISTS get_member_type'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Statement&quot;</span>;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> get_member_type;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'DELIMITER $$'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Statement&quot;</span>;
DELIMITER $$
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'CREATE FUNCTION get_member_type'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Statement&quot;</span>$$
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> get_member_type<span style="color: #66cc66;">&#40;</span>pv_member_id INT<span style="color: #66cc66;">&#41;</span> RETURNS INT 
BEGIN
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Define three local variables. */</span>
  DECLARE lv_contact_number  INT;
  DECLARE lv_member_type     CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;
  DECLARE lv_return_value    INT;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Define a dynamic cursor to count the number of foreign key values. */</span>  
  DECLARE contact_cursor CURSOR <span style="color: #993333; font-weight: bold;">FOR</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span>
    <span style="color: #993333; font-weight: bold;">FROM</span>   contact c
    <span style="color: #993333; font-weight: bold;">WHERE</span>  c<span style="color: #66cc66;">.</span>member_id <span style="color: #66cc66;">=</span> pv_member_id;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Define a dynamic cursor to find a key for an individual or group member type. */</span>
  DECLARE common_lookup_cursor CURSOR <span style="color: #993333; font-weight: bold;">FOR</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> common_lookup_id
    <span style="color: #993333; font-weight: bold;">FROM</span>   common_lookup
    <span style="color: #993333; font-weight: bold;">WHERE</span>  common_lookup_table <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'MEMBER'</span>
    <span style="color: #993333; font-weight: bold;">AND</span>    common_lookup_column <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'MEMBER_TYPE'</span>           
    <span style="color: #993333; font-weight: bold;">AND</span>    common_lookup_type <span style="color: #66cc66;">=</span> lv_member_type;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Get the number of foreign key values for a contact. */</span>    
  OPEN  contact_cursor;
  FETCH contact_cursor
  <span style="color: #993333; font-weight: bold;">INTO</span>  lv_contact_number;
  CLOSE contact_cursor;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Assign group membership when more than one foreign key value is found; and
     assign individual membership when only one foreign key value is found. */</span>  
  <span style="color: #993333; font-weight: bold;">IF</span> lv_contact_number <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">1</span> THEN
    <span style="color: #993333; font-weight: bold;">SET</span> lv_member_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'GROUP'</span>;
  ELSE
    <span style="color: #993333; font-weight: bold;">SET</span> lv_member_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'INDIVIDUAL'</span>;
  END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Get the correct surrogate primary key value. */</span>  
  OPEN  common_lookup_cursor;
  FETCH common_lookup_cursor
  <span style="color: #993333; font-weight: bold;">INTO</span>  lv_return_value;
  CLOSE common_lookup_cursor;
&nbsp;
  <span style="color: #808080; font-style: italic;">/* Return the correct primary key for use as a foreign key. */</span>  
  <span style="color: #993333; font-weight: bold;">RETURN</span> lv_return_value;
END;
$$
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'DELIMITER $$'</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;Statement&quot;</span>$$
DELIMITER ;</pre></td></tr></table></div>

</div>
</div>
</div>
<p style="clear:both">
<p>You can query the results of the update statement with the following.</p>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Change Confirmation Query</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
Expand this section to see the query that lets you examine the changes. It runs in either Oracle or MySQL without any changes.</p>
<div>
<div style="padding-left:30px" class="panelCode650">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>   m<span style="color: #66cc66;">.</span>member_id
<span style="color: #66cc66;">,</span>        dt<span style="color: #66cc66;">.</span>quantity
<span style="color: #66cc66;">,</span>        m<span style="color: #66cc66;">.</span>member_type
<span style="color: #66cc66;">,</span>        cl<span style="color: #66cc66;">.</span>common_lookup_type
<span style="color: #993333; font-weight: bold;">FROM</span>     member m <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>   member_id
                        <span style="color: #66cc66;">,</span>        COUNT<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> quantity
                        <span style="color: #993333; font-weight: bold;">FROM</span>     contact c
                        <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> c<span style="color: #66cc66;">.</span>member_id<span style="color: #66cc66;">&#41;</span> dt
<span style="color: #993333; font-weight: bold;">ON</span>       m<span style="color: #66cc66;">.</span>member_id <span style="color: #66cc66;">=</span> dt<span style="color: #66cc66;">.</span>member_id <span style="color: #993333; font-weight: bold;">JOIN</span> common_lookup cl
<span style="color: #993333; font-weight: bold;">ON</span>       m<span style="color: #66cc66;">.</span>member_type <span style="color: #66cc66;">=</span> cl<span style="color: #66cc66;">.</span>common_lookup_id
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> m<span style="color: #66cc66;">.</span>member_id;</pre></td></tr></table></div>

</div>
</div>
</div>
<p style="clear:both">
<p>As always, I look forward to helping and gaining insight.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/06/27/correlated-update-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Show indexes in Oracle</title>
		<link>http://blog.mclaughlinsoftware.com/2010/06/22/show-indexes-in-oracle/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/06/22/show-indexes-in-oracle/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 07:28:15 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle XE]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=4024</guid>
		<description><![CDATA[One of my students asked how you could show index from table_name; in Oracle. They were chagrined when I told them there wasn&#8217;t an equivalent command. Outside of using Quest&#8217;s Toad or Oracle SQL*Developer, you can query the data catalog, like so: -- SQL*Plus formatting commands. COLUMN index_name FORMAT A32 COLUMN column_position FORMAT 999 HEADING [...]]]></description>
			<content:encoded><![CDATA[<p>One of my students asked how you could <code>show index from <i>table_name</i>;</code> in Oracle. They were chagrined when I told them there wasn&#8217;t an equivalent command. Outside of using Quest&#8217;s Toad or Oracle SQL*Developer, you can query the data catalog, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- SQL*Plus formatting commands.</span>
<span style="color: #993333; font-weight: bold;">COLUMN</span> index_name FORMAT A32
<span style="color: #993333; font-weight: bold;">COLUMN</span> column_position FORMAT <span style="color: #cc66cc;">999</span> HEADING <span style="color: #ff0000;">&quot;COLUMN|POSITION&quot;</span>
<span style="color: #993333; font-weight: bold;">COLUMN</span> column_name FORMAT A32
&nbsp;
<span style="color: #808080; font-style: italic;">-- Ordinary query with a substitution variable.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> i<span style="color: #66cc66;">.</span>index_name
<span style="color: #66cc66;">,</span>      ic<span style="color: #66cc66;">.</span>column_position
<span style="color: #66cc66;">,</span>      ic<span style="color: #66cc66;">.</span>column_name
<span style="color: #993333; font-weight: bold;">FROM</span>   user_indexes i <span style="color: #993333; font-weight: bold;">JOIN</span> user_ind_columns ic
<span style="color: #993333; font-weight: bold;">ON</span>     i<span style="color: #66cc66;">.</span>index_name <span style="color: #66cc66;">=</span> ic<span style="color: #66cc66;">.</span>index_name
<span style="color: #993333; font-weight: bold;">WHERE</span>  i<span style="color: #66cc66;">.</span>table_name <span style="color: #66cc66;">=</span> UPPER<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&amp;input'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Naturally, this is a subset of what&#8217;s returned by the <code>show index from <i>table_name</i></code>; syntax. There is much more information in these tables but I only wanted to show an example.</p>
<p>The <code>UPPER</code> function command ensures that the table name is found in the database. Unless you&#8217;ve created a case sensitive object, they&#8217;re stored in uppercase strings.</p>
<p>While a single SQL statement works well, a little organization in PL/SQL makes it more readable. A <code>display_indexes</code> function provides that organization. It only displays <em>normal</em> indexes, not LOB indexes, and it depends on a schema-level collection of strings. This is the user-defined type (UDT) that I used for the collection.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE index_table <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">/</span></pre></div></div>

<p>The following is the definition of the function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> display_indexes
<span style="color: #66cc66;">&#40;</span> pv_table_name VARCHAR2 <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> INDEX_TABLE <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an iterator for the collection return variable.</span>
  index_counter  NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
  column_counter NUMBER;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare and initialize local collection variable as return type.</span>
  index_desc INDEX_TABLE :<span style="color: #66cc66;">=</span> index_table<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Get indexes.</span>
  CURSOR index_name <span style="color: #66cc66;">&#40;</span>cv_table_name VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span>   i<span style="color: #66cc66;">.</span>index_name
    <span style="color: #993333; font-weight: bold;">FROM</span>     user_indexes i
    <span style="color: #993333; font-weight: bold;">WHERE</span>    i<span style="color: #66cc66;">.</span>table_name <span style="color: #66cc66;">=</span> cv_table_name
    <span style="color: #993333; font-weight: bold;">AND</span>      i<span style="color: #66cc66;">.</span>index_type <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'NORMAL'</span>
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Get index columns.    </span>
  CURSOR index_columns <span style="color: #66cc66;">&#40;</span>cv_index_name VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span>   ic<span style="color: #66cc66;">.</span>column_position
    <span style="color: #66cc66;">,</span>        ic<span style="color: #66cc66;">.</span>column_name
    <span style="color: #993333; font-weight: bold;">FROM</span>     user_ind_columns ic
    <span style="color: #993333; font-weight: bold;">WHERE</span>    ic<span style="color: #66cc66;">.</span>index_name <span style="color: #66cc66;">=</span> cv_index_name
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
BEGIN
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Assign the table name to the collection.</span>
  index_desc<span style="color: #66cc66;">.</span>EXTEND;
  index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> UPPER<span style="color: #66cc66;">&#40;</span>pv_table_name<span style="color: #66cc66;">&#41;</span>;
  index_counter :<span style="color: #66cc66;">=</span> index_counter <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> index_name<span style="color: #66cc66;">&#40;</span>UPPER<span style="color: #66cc66;">&#40;</span>pv_table_name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> LOOP
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Assign the index name to the collection.</span>
    index_desc<span style="color: #66cc66;">.</span>EXTEND;
    index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> LPAD<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>index_name<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">+</span> LENGTH<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>index_name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Set column counter on entry to nested loop.</span>
    column_counter :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
    <span style="color: #993333; font-weight: bold;">FOR</span> j <span style="color: #993333; font-weight: bold;">IN</span> index_columns<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>index_name<span style="color: #66cc66;">&#41;</span> LOOP
&nbsp;
      <span style="color: #993333; font-weight: bold;">IF</span> column_counter <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> THEN
&nbsp;
        <span style="color: #808080; font-style: italic;">-- Increment the column counter, extend space, and concatenate to string.</span>
        column_counter :<span style="color: #66cc66;">=</span> column_counter <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
        index_desc<span style="color: #66cc66;">.</span>EXTEND;
        index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'('</span> <span style="color: #66cc66;">||</span> LOWER<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">.</span>column_name<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      ELSE
&nbsp;
        <span style="color: #808080; font-style: italic;">-- Add a subsequent column to the list.</span>
        index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">','</span> <span style="color: #66cc66;">||</span> LOWER<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">.</span>column_name<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    END LOOP;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Append a close parenthesis and incredment index counter.</span>
    index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> index_desc<span style="color: #66cc66;">&#40;</span>index_counter<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">')'</span>;
    index_counter :<span style="color: #66cc66;">=</span> index_counter <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Return the array.</span>
  <span style="color: #993333; font-weight: bold;">RETURN</span> index_desc;
END;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

<p>You can call the function with this syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> column_value <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">&quot;TRANSACTION INDEXES&quot;</span>
<span style="color: #993333; font-weight: bold;">FROM</span>   <span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#40;</span>display_indexes<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'TRANSACTION'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It returns the following formatted output for the <code>TRANSACTION</code> table, which is much nicer than the SQL output. Unfortunately, it will take more effort to place it on par with the <code>show index from <i>table_name</i>;</code> in MySQL.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">TRANSACTION INDEXES
------------------------------------------------------------------------------------------------------------------------------
TRANSACTION
  PK_TRANSACTION(transaction_id)
  UQ_TRANSACTION(rental_id,transaction_type,transaction_date,payment_method_type,payment_account_number,transaction_account)</pre></div></div>

<p>As always, I hope it helps folks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/06/22/show-indexes-in-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A \G Option for Oracle?</title>
		<link>http://blog.mclaughlinsoftware.com/2010/06/14/a-g-option-for-oracle/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/06/14/a-g-option-for-oracle/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 07:01:04 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle XE]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3977</guid>
		<description><![CDATA[The \G option in MySQL lets you display rows of data as sets with the columns on the left and the data on the write. I figured it would be fun to write those for Oracle when somebody pointed out that they weren&#8217;t out there in cyberspace (first page of a Google search ). I [...]]]></description>
			<content:encoded><![CDATA[<p>The <code>\G</code> option in MySQL lets you display rows of data as sets with the columns on the left and the data on the write. I figured it would be fun to write those for Oracle when somebody pointed out that they weren&#8217;t out there in cyberspace (first page of a Google search <img src='http://blog.mclaughlinsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ).</p>
<p>I started the program with a student&#8217;s code. I thought it a bit advanced for the student but didn&#8217;t check if he&#8217;d snagged it somewhere. Thanks to Niall Litchfield, I now know is came from an <a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1035431863958">earlier post</a> of Tom Kyte. It failed when returning a Blob, BFile, or CFile column.</p>
<p>Naturally, there are two ways to write this. One is a procedure and the other is the function. This post contains both. The procedure is limited because of potential buffer overflows associated with the <code>DBMS_OUTPUT</code> package&#8217;s display. A function isn&#8217;t limited because you can return a collection from the function.</p>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Required setup to use the <code>DBMS_SQL</code> package</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
<p>The <code>DBMS_SQL</code> package requires permissions. There are two ways to provide those permissions. One is more secure and sensible in a production system and the other is great in a development test system.</p>
<p style="clear: both">
<div style="margin-left:20px">
<p><span sytle="font-size:115%"><strong><em>Production or Test System</em></strong></span></p>
<p>If this is a production system, you probably want to grant permissions only to the <code>SYSTEM</code> schema. This follows the practice of narrowing access to powerful features and control systems.</p>
<p>The first step requires the <code>SYS</code> user to grant permissions and authority to re-grant to individual users. You connect as the privileged user, like:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">sqlplus <span style="color: #66cc66;">/</span> <span style="color: #993333; font-weight: bold;">AS</span> sysdba</pre></div></div>

</div>
<p>When connected as the <code>SYS</code>, you run the following two commands:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> dbms_sys_sql <span style="color: #993333; font-weight: bold;">TO</span> system;
<span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> dbms_sql <span style="color: #993333; font-weight: bold;">TO</span> system;</pre></div></div>

</div>
<p>You should then define the procedure or function as a <code>CURRENT_USER</code> module. This type of module is known as an <em>invoker&#8217;s right</em> program. The code is owned by the <code>SYSTEM</code> schema but you run it on your own objects in your less privileged schema.</p>
<p>You can do that by replacing the function and procedure headers with these:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> PROCEDURE display_vertical
<span style="color: #66cc66;">&#40;</span> table_name VARCHAR2<span style="color: #66cc66;">,</span> where_clause VARCHAR2 <span style="color: #66cc66;">&#41;</span> AUTHID CURRENT_USER <span style="color: #993333; font-weight: bold;">IS</span></pre></div></div>

</div>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> vertical_query
<span style="color: #66cc66;">&#40;</span> table_name VARCHAR2<span style="color: #66cc66;">,</span> where_clause VARCHAR2 <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> query_result
AUTHID CURRENT_USER <span style="color: #993333; font-weight: bold;">IS</span></pre></div></div>

</div>
<p>After you compile the procedure and function in the <code>SYSTEM</code> schema, you should grant access to a schema (more restricted) or public (as generic tools). You should also create synonyms. The following commands assume you want to deploy these as generic tools. As the <code>SYSTEM</code> user, it grants privileges and then creates public synonyms.</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Grant privileges.</span>
<span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> display_vertical <span style="color: #993333; font-weight: bold;">TO</span> PUBLIC;
<span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> vertical_query <span style="color: #993333; font-weight: bold;">TO</span> PUBLIC;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Create public synonyms.</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> PUBLIC SYNONYM display_vertical <span style="color: #993333; font-weight: bold;">FOR</span> system<span style="color: #66cc66;">.</span>display_vertical;
<span style="color: #993333; font-weight: bold;">CREATE</span> PUBLIC SYNONYM vertical_query <span style="color: #993333; font-weight: bold;">FOR</span> system<span style="color: #66cc66;">.</span>vertical_query;</pre></div></div>

</div>
<p>You should now be able to call these from any schema to work with their own tables and views.</p>
<p><span sytle="font-size:115%"><strong><em>Student Development System</em></strong></span></p>
<p>If this is a test system and you&#8217;re new to Oracle, the following should help you. This shows you how to implement these in a <em>Definer&#8217;s right</em> model, inside a <code>STUDENT</code> schema.</p>
<p>This isn&#8217;t a secure design, but it allows you to keep your testing limited to a <code>STUDENT</code> schema. When these permissions aren&#8217;t granted the examples won&#8217;t work at all.</p>
<p>The first step requires the <code>SYS</code> user to grant permissions and authority to re-grant to individual users. You connect as the privileged user, like:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">sqlplus <span style="color: #66cc66;">/</span> <span style="color: #993333; font-weight: bold;">AS</span> sysdba</pre></div></div>

</div>
<p>When connected as the <code>SYS</code>, you run the following two commands:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> dbms_sys_sql <span style="color: #993333; font-weight: bold;">TO</span> system <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">OPTION</span>;
<span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> dbms_sql <span style="color: #993333; font-weight: bold;">TO</span> system <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">OPTION</span>;</pre></div></div>

</div>
<p>You don&#8217;t have to exit to reconnect as the <code>SYSTEM</code> user. Just type the following at the <code>SQL</code> command prompt (substitute your password <img src='http://blog.mclaughlinsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ).</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">connect system<span style="color: #66cc66;">/</span>password</pre></div></div>

</div>
<p>When connected as the <code>SYSTEM</code> user, you run the following two commands:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> dbms_sys_sql <span style="color: #993333; font-weight: bold;">TO</span> student;
<span style="color: #993333; font-weight: bold;">GRANT</span> EXECUTE <span style="color: #993333; font-weight: bold;">ON</span> dbms_sql <span style="color: #993333; font-weight: bold;">TO</span> student;</pre></div></div>

</div>
<p>You should now be able to compile the function and procedure.</p>
</div>
</div>
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Procedure for <code>\G</code> output</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
<p>The procedure nice because there&#8217;s only a dependency on the buffer size for the <code>DBMS_OUTPUT</code> package. The procedure only returns column values that are printable at the console, and it only returns the first 40 characters of long text strings.
</p>
<p style="clear: both">
<div style="margin-left:20px">
<p>Here&#8217;s the procedure definition:</p>
<div style="code-panel650">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Create procedure.</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> PROCEDURE display_vertical
<span style="color: #66cc66;">&#40;</span> table_name VARCHAR2<span style="color: #66cc66;">,</span> where_clause VARCHAR2 <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Open a cursor for a query against all columns in a table. </span>
  base_stmt  INTEGER :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>open_cursor;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Open a cursor for a dynamically constructed query, which excludes</span>
  <span style="color: #808080; font-style: italic;">-- any non-displayable columns with text.</span>
  stmt  INTEGER :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>open_cursor;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare local variables.</span>
  colValue   VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;      <span style="color: #808080; font-style: italic;">-- Declare a maximum string length for column values.</span>
  <span style="color: #993333; font-weight: bold;">STATUS</span>     INTEGER;             <span style="color: #808080; font-style: italic;">-- Declare a variable to hold acknowledgement of DBMS_SQL.EXECUTE</span>
  tableDesc  dbms_sql<span style="color: #66cc66;">.</span>desc_tab2;  <span style="color: #808080; font-style: italic;">-- Declare a table to hold metadata for the queries.</span>
  colCount   NUMBER;              <span style="color: #808080; font-style: italic;">-- Declare a variable for the column count.</span>
  rowIndex   NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;         <span style="color: #808080; font-style: italic;">-- for displaying the row number retrieved from the cursor</span>
  colLength  NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;         <span style="color: #808080; font-style: italic;">-- for keeping track of the length of the longest column name</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare local variable for the dynamically constructed query.</span>
  dynamic_stmt VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'SELECT '</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a bad table name, raised by a call to</span>
  <span style="color: #808080; font-style: italic;">-- the dbms_assert.qualified_sql_name function.</span>
  table_name_error EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>table_name_error<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">942</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare exception handlers for bad WHERE clause statements.</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a missing WHERE keyword.</span>
  missing_keyword EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>missing_keyword<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">933</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a bad relational operator.</span>
  invalid_relational_operator EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>invalid_relational_operator<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">920</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a bad column name.</span>
  invalid_identifier EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>invalid_identifier<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">904</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a missing backquoted apostrophe.</span>
  misquoted_string EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>misquoted_string<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1756</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare a function that replaces non-displayable values with text messages.</span>
  <span style="color: #993333; font-weight: bold;">FUNCTION</span> check_column<span style="color: #66cc66;">&#40;</span> p_name   VARCHAR2
                       <span style="color: #66cc66;">,</span> p_type   NUMBER <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> VARCHAR2 <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Return column name or literal value.</span>
    retval VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;			   
&nbsp;
  BEGIN
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Find strings, numbers, dates, timestamps, rowids and replace non-display values.</span>
    <span style="color: #993333; font-weight: bold;">IF</span> p_type <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">69</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">96</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">101</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">112</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">178</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">179</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">180</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">181</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">231</span><span style="color: #66cc66;">&#41;</span> THEN
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Assign the column name for a displayable column value.</span>
      retval :<span style="color: #66cc66;">=</span> p_name;
&nbsp;
    ELSE
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Re-assign string literals for column names where values aren't displayable.      </span>
      <span style="color: #993333; font-weight: bold;">SELECT</span> DECODE<span style="color: #66cc66;">&#40;</span>p_type<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">23</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'RAW not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">105</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'MLSLABEL not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">106</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'MLSLABEL not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">113</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'BLOB not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">114</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'BFILE not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">115</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'CFILE not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'UNDEFINED not displayable.'</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> retval
      <span style="color: #993333; font-weight: bold;">FROM</span> dual;
&nbsp;
    END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Return the column name or a apostrophe delimited string literal.</span>
    <span style="color: #993333; font-weight: bold;">RETURN</span> retval;
  END check_column;
&nbsp;
BEGIN
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Prepare unfiltered display cursor.</span>
  dbms_sql<span style="color: #66cc66;">.</span>parse<span style="color: #66cc66;">&#40;</span>base_stmt<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'SELECT * FROM '</span> <span style="color: #66cc66;">||</span> dbms_assert<span style="color: #66cc66;">.</span>simple_sql_name<span style="color: #66cc66;">&#40;</span>table_name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> where_clause<span style="color: #66cc66;">,</span> dbms_sql<span style="color: #66cc66;">.</span>native<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Describe the table structure:</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  <span style="color: #808080; font-style: italic;">--  1. Store metadata in tableDesc</span>
  <span style="color: #808080; font-style: italic;">--  2. Store the number of columns in colCount</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  dbms_sql<span style="color: #66cc66;">.</span>describe_columns2<span style="color: #66cc66;">&#40;</span>base_stmt<span style="color: #66cc66;">,</span> colCount<span style="color: #66cc66;">,</span> tableDesc<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define individual columns and assign value to colValue variable.</span>
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>colCount LOOP
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Define columns for each column returned into tableDesc.</span>
    dbms_sql<span style="color: #66cc66;">.</span>define_column<span style="color: #66cc66;">&#40;</span>base_stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Find the length of the longest column name.</span>
    <span style="color: #993333; font-weight: bold;">IF</span> LENGTH<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> colLength THEN
      colLength :<span style="color: #66cc66;">=</span> LENGTH<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">&#41;</span>;
    END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Replace non-displayable column values with displayable values.</span>
    <span style="color: #993333; font-weight: bold;">IF</span> i <span style="color: #66cc66;">&lt;</span> colCount THEN
      dynamic_stmt :<span style="color: #66cc66;">=</span> dynamic_stmt <span style="color: #66cc66;">||</span> check_column<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' AS '</span>
                   <span style="color: #66cc66;">||</span> tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', '</span>;
    ELSE
      dynamic_stmt :<span style="color: #66cc66;">=</span> dynamic_stmt <span style="color: #66cc66;">||</span> check_column<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' AS '</span>
                   <span style="color: #66cc66;">||</span> tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'FROM '</span> <span style="color: #66cc66;">||</span> dbms_assert<span style="color: #66cc66;">.</span>simple_sql_name<span style="color: #66cc66;">&#40;</span>table_name<span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> where_clause;
    END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Provide conditional debugging instruction that displays dynamically created query.</span>
  $IF $$DEBUG <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> $THEN
     dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>dynamic_stmt<span style="color: #66cc66;">&#41;</span>;
  $END
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Prepare unfiltered display cursor. </span>
  dbms_sql<span style="color: #66cc66;">.</span>parse<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> dynamic_stmt<span style="color: #66cc66;">,</span> dbms_sql<span style="color: #66cc66;">.</span>native<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Describe the table structure:</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  <span style="color: #808080; font-style: italic;">--  1. Store metadata in tableDesc (reuse of existing variable)</span>
  <span style="color: #808080; font-style: italic;">--  2. Store the number of columns in colCount</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  dbms_sql<span style="color: #66cc66;">.</span>describe_columns2<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> colCount<span style="color: #66cc66;">,</span> tableDesc<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define individual columns and assign value to colValue variable.</span>
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>colCount LOOP
    dbms_sql<span style="color: #66cc66;">.</span>define_column<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Execute the dynamic cursor.</span>
  <span style="color: #993333; font-weight: bold;">STATUS</span> :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>execute<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Fetch the results, row-by-row.</span>
  WHILE dbms_sql<span style="color: #66cc66;">.</span>fetch_rows<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> LOOP
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Reset row counter for display purposes.</span>
    rowIndex :<span style="color: #66cc66;">=</span> rowIndex <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'********************************** '</span> <span style="color: #66cc66;">||</span> rowIndex <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'. row **********************************'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- For each column, print left-aligned column names and values.</span>
    <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>colCount LOOP
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Limit display of long text.  </span>
      <span style="color: #993333; font-weight: bold;">IF</span> tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_type <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">96</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">112</span><span style="color: #66cc66;">&#41;</span> THEN
&nbsp;
        <span style="color: #808080; font-style: italic;">-- Display 40 character substrings of long text.  </span>
  	dbms_sql<span style="color: #66cc66;">.</span>column_value<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">&#41;</span>;
        dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>RPAD<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span> colLength<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' : '</span> <span style="color: #66cc66;">||</span> SUBSTR<span style="color: #66cc66;">&#40;</span>colValue<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	  
&nbsp;
      ELSE
&nbsp;
        <span style="color: #808080; font-style: italic;">-- Display full value as character string.  </span>
        dbms_sql<span style="color: #66cc66;">.</span>column_value<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">&#41;</span>;
        dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>RPAD<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span> colLength<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' : '</span> <span style="color: #66cc66;">||</span> colValue<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    END LOOP;
&nbsp;
  END LOOP;
&nbsp;
EXCEPTION
  <span style="color: #808080; font-style: italic;">-- Customer error handlers.</span>
  WHEN table_name_error THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN invalid_relational_operator THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN invalid_identifier THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN missing_keyword THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN misquoted_string THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN OTHERS THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
END;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

</div>
<p>You can run the procedure with the following syntax:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXECUTE display_vertical<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ITEM'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'WHERE item_title LIKE '</span><span style="color: #ff0000;">'Star%'</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</div>
<p>It&#8217;ll return the following display of data:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">**********************************</span> 1<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1002</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span>02392
ITEM_TYPE         : <span style="color: #cc66cc;">1011</span>
ITEM_TITLE        : Star Wars I
ITEM_SUBTITLE     : Phantom Menace
ITEM_RATING       : PG
ITEM_RELEASE_DATE : 04<span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">99</span>
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         :
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">**********************************</span> 2<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1003</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">5615</span>
ITEM_TYPE         : <span style="color: #cc66cc;">1010</span>
ITEM_TITLE        : Star Wars II
ITEM_SUBTITLE     : Attack of the Clones
ITEM_RATING       : PG
ITEM_RELEASE_DATE : <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span>02
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         :
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">**********************************</span> 3<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1004</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span>05539
ITEM_TYPE         : <span style="color: #cc66cc;">1011</span>
ITEM_TITLE        : Star Wars II
ITEM_SUBTITLE     : Attack of the Clones
ITEM_RATING       : PG
ITEM_RELEASE_DATE : <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span>02
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         : This <span style="color: #993333; font-weight: bold;">IS</span> designed <span style="color: #993333; font-weight: bold;">TO</span> be a long enough str
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">**********************************</span> 4<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1005</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">20309</span>
ITEM_TYPE         : <span style="color: #cc66cc;">1011</span>
ITEM_TITLE        : Star Wars III
ITEM_SUBTITLE     : Revenge of the Sith
ITEM_RATING       : PG13
ITEM_RELEASE_DATE : <span style="color: #cc66cc;">19</span><span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span>05
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         :
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span></pre></div></div>

</div>
</div>
</div>
<p style="clear:both">
<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Function for <code>\G</code> output</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
<p>The function is the best solution. It does have a dependency on a user-defined type (UDT). The function, like the procedure, only returns column values that are printable at the console. It also parses the first 40 characters from long text strings.
</p>
<p style="clear: both">
<div style="margin-left:20px">
<p>Before you create the function, you must create a UDT collection variable. The following syntax creates a schema-level UDT.</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> TYPE query_result <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE</span> OF VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">77</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">/</span></pre></div></div>

</div>
<p>Here&#8217;s the function definition:</p>
<div style="code-panel650">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> vertical_query
<span style="color: #66cc66;">&#40;</span> table_name VARCHAR2<span style="color: #66cc66;">,</span> where_clause VARCHAR2 <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> query_result <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Open a cursor for a query against all columns in a table. </span>
  base_stmt	INTEGER :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>open_cursor;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Open a cursor for a dynamically constructed query, which excludes</span>
  <span style="color: #808080; font-style: italic;">-- any non-displayable columns with text.</span>
  stmt      INTEGER :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>open_cursor;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare local variables.</span>
  colValue	VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;      <span style="color: #808080; font-style: italic;">-- Declare a maximum string length for column values.</span>
  <span style="color: #993333; font-weight: bold;">STATUS</span>		INTEGER;             <span style="color: #808080; font-style: italic;">-- Declare a variable to hold acknowledgement of DBMS_SQL.EXECUTE</span>
  tableDesc	dbms_sql<span style="color: #66cc66;">.</span>desc_tab2;  <span style="color: #808080; font-style: italic;">-- Declare a table to hold metadata for the queries.</span>
  colCount	NUMBER;              <span style="color: #808080; font-style: italic;">-- Declare a variable for the column count.</span>
  rowIndex	NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;         <span style="color: #808080; font-style: italic;">-- for displaying the row number retrieved from the cursor</span>
  colLength	NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;         <span style="color: #808080; font-style: italic;">-- for keeping track of the length of the longest column name</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare local variable for the dynamically constructed query.</span>
  dynamic_stmt VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'SELECT '</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare a index for the return collection.</span>
  rsIndex NUMBER :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare a collection variable and instantiate the collection.</span>
  result_set QUERY_RESULT :<span style="color: #66cc66;">=</span> query_result<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a bad table name, raised by a call to</span>
  <span style="color: #808080; font-style: italic;">-- the dbms_assert.qualified_sql_name function.</span>
  table_name_error EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>table_name_error<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">942</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare exception handlers for bad WHERE clause statements.</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a missing WHERE keyword.</span>
  missing_keyword EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>missing_keyword<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">933</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a bad relational operator.</span>
  invalid_relational_operator EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>invalid_relational_operator<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">920</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a bad column name.</span>
  invalid_identifier EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>invalid_identifier<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">904</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare an exception for a missing backquoted apostrophe.</span>
  misquoted_string EXCEPTION;
  PRAGMA EXCEPTION_INIT<span style="color: #66cc66;">&#40;</span>misquoted_string<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1756</span><span style="color: #66cc66;">&#41;</span>;  
&nbsp;
  <span style="color: #808080; font-style: italic;">-- ------------------------------------------------------------------</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Declare a function that replaces non-displayable values with text messages.</span>
  <span style="color: #993333; font-weight: bold;">FUNCTION</span> check_column<span style="color: #66cc66;">&#40;</span> p_name   VARCHAR2
                       <span style="color: #66cc66;">,</span> p_type   NUMBER <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURN</span> VARCHAR2 <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Return column name or literal value.</span>
    retval VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;			   
&nbsp;
  BEGIN
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Find strings, numbers, dates, timestamps, rowids and replace non-display values.</span>
    <span style="color: #993333; font-weight: bold;">IF</span> p_type <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">69</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">96</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">101</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">112</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">178</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">179</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">180</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">181</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">231</span><span style="color: #66cc66;">&#41;</span> THEN
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Assign the column name for a displayable column value.</span>
      retval :<span style="color: #66cc66;">=</span> p_name;
&nbsp;
    ELSE
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Re-assign string literals for column names where values aren't displayable.      </span>
      <span style="color: #993333; font-weight: bold;">SELECT</span> DECODE<span style="color: #66cc66;">&#40;</span>p_type<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">23</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'RAW not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">105</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'MLSLABEL not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">106</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'MLSLABEL not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">113</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'BLOB not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">114</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'BFILE not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #cc66cc;">115</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'CFILE not displayable.'</span><span style="color: #ff0000;">''</span>
                          <span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">'UNDEFINED not displayable.'</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #993333; font-weight: bold;">INTO</span> retval
      <span style="color: #993333; font-weight: bold;">FROM</span> dual;
&nbsp;
    END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Return the column name or a apostrophe delimited string literal.</span>
    <span style="color: #993333; font-weight: bold;">RETURN</span> retval;
  END check_column;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- ------------------------------------------------------------------</span>
&nbsp;
BEGIN
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Prepare unfiltered display cursor.</span>
	dbms_sql<span style="color: #66cc66;">.</span>parse<span style="color: #66cc66;">&#40;</span>base_stmt<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'SELECT * FROM '</span> <span style="color: #66cc66;">||</span> dbms_assert<span style="color: #66cc66;">.</span>simple_sql_name<span style="color: #66cc66;">&#40;</span>table_name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> where_clause<span style="color: #66cc66;">,</span> dbms_sql<span style="color: #66cc66;">.</span>native<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Describe the table structure:</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  <span style="color: #808080; font-style: italic;">--  1. Store metadata in tableDesc</span>
  <span style="color: #808080; font-style: italic;">--  2. Store the number of columns in colCount</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  dbms_sql<span style="color: #66cc66;">.</span>describe_columns2<span style="color: #66cc66;">&#40;</span>base_stmt<span style="color: #66cc66;">,</span> colCount<span style="color: #66cc66;">,</span> tableDesc<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define individual columns and assign value to colValue variable.</span>
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>colCount LOOP
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Define columns for each column returned into tableDesc.</span>
    dbms_sql<span style="color: #66cc66;">.</span>define_column<span style="color: #66cc66;">&#40;</span>base_stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Find the length of the longest column name.</span>
    <span style="color: #993333; font-weight: bold;">IF</span> LENGTH<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> colLength THEN
      colLength :<span style="color: #66cc66;">=</span> LENGTH<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">&#41;</span>;
    END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Replace non-displayable column values with displayable values.</span>
    <span style="color: #993333; font-weight: bold;">IF</span> i <span style="color: #66cc66;">&lt;</span> colCount THEN
      dynamic_stmt :<span style="color: #66cc66;">=</span> dynamic_stmt <span style="color: #66cc66;">||</span> check_column<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' AS '</span>
                   <span style="color: #66cc66;">||</span> tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', '</span>;
    ELSE
      dynamic_stmt :<span style="color: #66cc66;">=</span> dynamic_stmt <span style="color: #66cc66;">||</span> check_column<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_type<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' AS '</span>
                   <span style="color: #66cc66;">||</span> tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'FROM '</span> <span style="color: #66cc66;">||</span> dbms_assert<span style="color: #66cc66;">.</span>simple_sql_name<span style="color: #66cc66;">&#40;</span>table_name<span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">||</span> where_clause;
    END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Provide conditional debugging instruction that displays dynamically created query.</span>
  $IF $$DEBUG <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> $THEN
     dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>dynamic_stmt<span style="color: #66cc66;">&#41;</span>;
  $END
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Prepare unfiltered display cursor. </span>
  dbms_sql<span style="color: #66cc66;">.</span>parse<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> dynamic_stmt<span style="color: #66cc66;">,</span> dbms_sql<span style="color: #66cc66;">.</span>native<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Describe the table structure:</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  <span style="color: #808080; font-style: italic;">--  1. Store metadata in tableDesc (reuse of existing variable)</span>
  <span style="color: #808080; font-style: italic;">--  2. Store the number of columns in colCount</span>
  <span style="color: #808080; font-style: italic;">-- --------------------------------------------------------</span>
  dbms_sql<span style="color: #66cc66;">.</span>describe_columns2<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> colCount<span style="color: #66cc66;">,</span> tableDesc<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Define individual columns and assign value to colValue variable.</span>
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>colCount LOOP
    dbms_sql<span style="color: #66cc66;">.</span>define_column<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Execute the dynamic cursor.</span>
  <span style="color: #993333; font-weight: bold;">STATUS</span> :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>execute<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Fetch the results, row-by-row.</span>
  WHILE dbms_sql<span style="color: #66cc66;">.</span>fetch_rows<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> LOOP
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Reset row counter for output display purposes.</span>
    rowIndex :<span style="color: #66cc66;">=</span> rowIndex <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- Increment the counter for the collection and extend space before assignment.</span>
    rsIndex :<span style="color: #66cc66;">=</span> rsIndex <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
    result_set<span style="color: #66cc66;">.</span>EXTEND;
    result_set<span style="color: #66cc66;">&#40;</span>rsIndex<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'********************************** '</span> <span style="color: #66cc66;">||</span> rowIndex <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'. row **********************************'</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">-- For each column, print left-aligned column names and values.</span>
    <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>colCount LOOP
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Increment the counter for the collection and extend space before assignment.</span>
      rsIndex :<span style="color: #66cc66;">=</span> rsIndex <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
      result_set<span style="color: #66cc66;">.</span>EXTEND;
&nbsp;
      <span style="color: #808080; font-style: italic;">-- Limit display of long text.  </span>
      <span style="color: #993333; font-weight: bold;">IF</span> tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_type <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">96</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">112</span><span style="color: #66cc66;">&#41;</span> THEN
&nbsp;
        <span style="color: #808080; font-style: italic;">-- Display 40 character substrings of long text.  </span>
        dbms_sql<span style="color: #66cc66;">.</span>column_value<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">&#41;</span>;
        result_set<span style="color: #66cc66;">&#40;</span>rsIndex<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> RPAD<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span> colLength<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' : '</span> <span style="color: #66cc66;">||</span> SUBSTR<span style="color: #66cc66;">&#40;</span>colValue<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;	  
&nbsp;
      ELSE
&nbsp;
        <span style="color: #808080; font-style: italic;">-- Display full value as character string.  </span>
        dbms_sql<span style="color: #66cc66;">.</span>column_value<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">,</span> colValue<span style="color: #66cc66;">&#41;</span>;
        result_set<span style="color: #66cc66;">&#40;</span>rsIndex<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> RPAD<span style="color: #66cc66;">&#40;</span>tableDesc<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>col_name<span style="color: #66cc66;">,</span> colLength<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' : '</span> <span style="color: #66cc66;">||</span> colValue;
&nbsp;
      END <span style="color: #993333; font-weight: bold;">IF</span>;
&nbsp;
    END LOOP;
&nbsp;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Increment the counter for the collection and extend space before assignment.</span>
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> 1<span style="color: #66cc66;">..</span>3 LOOP
    rsIndex :<span style="color: #66cc66;">=</span> rsIndex <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
    result_set<span style="color: #66cc66;">.</span>EXTEND;
&nbsp;
    CASE i
      WHEN <span style="color: #cc66cc;">1</span> THEN
        result_set<span style="color: #66cc66;">&#40;</span>rsIndex<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'****************************************************************************'</span>;
      WHEN <span style="color: #cc66cc;">2</span> THEN
        result_set<span style="color: #66cc66;">&#40;</span>rsIndex<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> CHR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;    
      WHEN <span style="color: #cc66cc;">3</span> THEN
        result_set<span style="color: #66cc66;">&#40;</span>rsIndex<span style="color: #66cc66;">&#41;</span> :<span style="color: #66cc66;">=</span> rowIndex <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' rows in set'</span>;    
    END CASE;
  END LOOP;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Return collection.</span>
  <span style="color: #993333; font-weight: bold;">RETURN</span> result_set;
EXCEPTION
  <span style="color: #808080; font-style: italic;">-- Customer error handlers, add specialized text or collapse into one with the OTHERS catchall.</span>
  WHEN table_name_error THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN invalid_relational_operator THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN invalid_identifier THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN missing_keyword THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN misquoted_string THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
  WHEN OTHERS THEN
    dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>SQLERRM<span style="color: #66cc66;">&#41;</span>;
END;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

</div>
<p>Before you attempt to run the function, you should set two Oracle SQL*Plus environment commands. One suppresses a message saying what just ran, and the other removes column headers. Clearly, the output is sufficient and the headers are clutter. You set these, as noted below:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> FEEDBACK OFF
<span style="color: #993333; font-weight: bold;">SET</span> PAGESIZE <span style="color: #cc66cc;">0</span></pre></div></div>

</div>
<p>You can run the function with the following syntax (the <code>COLUMN_VALUE</code> is the standard name returned from a scalar schema-level collection.</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> column_value
<span style="color: #993333; font-weight: bold;">FROM</span>   <span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#40;</span>vertical_query<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ITEM'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'WHERE item_title LIKE '</span><span style="color: #ff0000;">'Star%'</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</div>
<p>It&#8217;ll return the following display of data:</p>
<div style="code-panel650">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">**********************************</span> 1<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1002</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span>02392
ITEM_TYPE         : <span style="color: #cc66cc;">1011</span>
ITEM_TITLE        : Star Wars I
ITEM_SUBTITLE     : Phantom Menace
ITEM_RATING       : PG
ITEM_RELEASE_DATE : 04<span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">99</span>
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         :
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">**********************************</span> 2<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1003</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">5615</span>
ITEM_TYPE         : <span style="color: #cc66cc;">1010</span>
ITEM_TITLE        : Star Wars II
ITEM_SUBTITLE     : Attack of the Clones
ITEM_RATING       : PG
ITEM_RELEASE_DATE : <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span>02
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         :
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">**********************************</span> 3<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1004</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span>05539
ITEM_TYPE         : <span style="color: #cc66cc;">1011</span>
ITEM_TITLE        : Star Wars II
ITEM_SUBTITLE     : Attack of the Clones
ITEM_RATING       : PG
ITEM_RELEASE_DATE : <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span>02
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         : This <span style="color: #993333; font-weight: bold;">IS</span> designed <span style="color: #993333; font-weight: bold;">TO</span> be a long enough str
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">**********************************</span> 4<span style="color: #66cc66;">.</span> row <span style="color: #66cc66;">**********************************</span>
ITEM_ID           : <span style="color: #cc66cc;">1005</span>
ITEM_BARCODE      : <span style="color: #cc66cc;">24543</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">20309</span>
ITEM_TYPE         : <span style="color: #cc66cc;">1011</span>
ITEM_TITLE        : Star Wars III
ITEM_SUBTITLE     : Revenge of the Sith
ITEM_RATING       : PG13
ITEM_RELEASE_DATE : <span style="color: #cc66cc;">19</span><span style="color: #66cc66;">-</span>MAY<span style="color: #66cc66;">-</span>05
CREATED_BY        : <span style="color: #cc66cc;">3</span>
CREATION_DATE     : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
LAST_UPDATED_BY   : <span style="color: #cc66cc;">3</span>
LAST_UPDATE_DATE  : 09<span style="color: #66cc66;">-</span>JUN<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span>
ITEM_DESC         :
DISPLAY_PHOTO     : BLOB <span style="color: #993333; font-weight: bold;">NOT</span> displayable<span style="color: #66cc66;">.</span>
<span style="color: #66cc66;">****************************************************************************</span></pre></div></div>

</div>
</div>
</div>
<p>As usual, I hope this helps folks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/06/14/a-g-option-for-oracle/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>A couple DBMS_SQL limits</title>
		<link>http://blog.mclaughlinsoftware.com/2010/06/12/a-couple-dbms_sql-limits/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/06/12/a-couple-dbms_sql-limits/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 05:43:26 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle XE]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[clob]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3945</guid>
		<description><![CDATA[While developing a dynamic SQL example in Oracle 11g that builds a query based on available display columns, I found two interesting error messages. Now instead of noting it for the umpteenth time, I&#8217;m documenting it for everybody. The error messages are generated when this DBMS_SQL package&#8217;s statement is a SELECT statement, and is executed [...]]]></description>
			<content:encoded><![CDATA[<p>While developing a dynamic SQL example in Oracle 11<em>g</em> that builds <a href="http://blog.mclaughlinsoftware.com/2010/06/14/a-g-option-for-oracle/">a query based on available display columns</a>, I found two interesting error messages. Now instead of noting it for the umpteenth time, I&#8217;m documenting it for everybody. The error messages are generated when this <code>DBMS_SQL</code> package&#8217;s statement is a <code>SELECT</code> statement, and is executed with either a <code>BLOB</code>, <code>BFILE</code> or <code>CFILE</code> column in the list of returned columns.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>26
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">STATUS</span> :<span style="color: #66cc66;">=</span> dbms_sql<span style="color: #66cc66;">.</span>execute<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><span sytle="font-size:125%"><strong><code>BLOB</code> data type</strong></span></p>
<p>You get the following error when a column in the query has a <code>BLOB</code> data type. If you alter the query to exclude the column, no error occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN test<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DEMO'</span><span style="color: #66cc66;">&#41;</span>; END;
<span style="color: #66cc66;">*</span>
ERROR at line <span style="color: #cc66cc;">1</span>:
ORA<span style="color: #66cc66;">-</span>00932: inconsistent datatypes: expected NUMBER got BLOB
ORA<span style="color: #66cc66;">-</span>06512: at <span style="color: #ff0000;">&quot;SYS.DBMS_SQL&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #cc66cc;">1575</span>
ORA<span style="color: #66cc66;">-</span>06512: at <span style="color: #ff0000;">&quot;STUDENT.TEST&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #cc66cc;">26</span>
ORA<span style="color: #66cc66;">-</span>06512: at line <span style="color: #cc66cc;">1</span></pre></div></div>

<p><span sytle="font-size:125%"><strong><code>BFILE</code> or <code>CFILE</code> data type</strong></span></p>
<p>You get the following error when a column in the query has a <code>BFILE</code> or <code>CFILE</code> data type. If you alter the query to exclude the column, no error occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN test<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'DEMO'</span><span style="color: #66cc66;">&#41;</span>; END;
<span style="color: #66cc66;">*</span>
ERROR at line <span style="color: #cc66cc;">1</span>:
ORA<span style="color: #66cc66;">-</span>00932: inconsistent datatypes: expected NUMBER got FILE
ORA<span style="color: #66cc66;">-</span>06512: at <span style="color: #ff0000;">&quot;SYS.DBMS_SQL&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #cc66cc;">1575</span>
ORA<span style="color: #66cc66;">-</span>06512: at <span style="color: #ff0000;">&quot;STUDENT.TEST&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #cc66cc;">26</span>
ORA<span style="color: #66cc66;">-</span>06512: at line <span style="color: #cc66cc;">1</span></pre></div></div>

<p>It&#8217;s never a joy to debug the <code>DBMS_SQL</code> package, at least it&#8217;s never a joy for me. I hope this helps somebody sort out an issue more quickly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/06/12/a-couple-dbms_sql-limits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>User-defined SYS_CONTEXT</title>
		<link>http://blog.mclaughlinsoftware.com/2010/05/05/user-defined-sys_context/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/05/05/user-defined-sys_context/#comments</comments>
		<pubDate>Wed, 05 May 2010 14:15:06 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle XE]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3730</guid>
		<description><![CDATA[Looking through an error on the web, I notices that the solution is nested in Ask Tom. That&#8217;s true for so many solutions, but they likewise have long discussions like this one in the OraFAQ Forum. It seems that most folks search on is the following. The problem appears to be linked to attempts to [...]]]></description>
			<content:encoded><![CDATA[<p>Looking through an error on the web, I notices that the solution is <a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1288401763279">nested in Ask Tom</a>. That&#8217;s true for so many solutions, but they likewise have long discussions like <a href="http://www.orafaq.com/forum/t/94764/0/">this one in the OraFAQ Forum</a>.</p>
<p>It seems that most folks search on is the following. The problem appears to be linked to attempts to call the <code>DBMS_SESSION.SET_CONTEXT</code> directly in their code, instead of through a predefined procedure. The procedure is generally inside a security package in a security schema for reference.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">begin
<span style="color: #66cc66;">*</span>
ERROR at line <span style="color: #cc66cc;">1</span>:
ORA<span style="color: #66cc66;">-</span>01031: insufficient privileges
ORA<span style="color: #66cc66;">-</span>06512: at <span style="color: #ff0000;">&quot;SYS.DBMS_SESSION&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #cc66cc;">94</span>
ORA<span style="color: #66cc66;">-</span>06512: at line <span style="color: #cc66cc;">2</span></pre></div></div>

<p>I figured it might help to provide a simple example because I use VPDs in my second database class, and this is where some of my students get hung up. It strikes me others in the Oracle community may get stuck here too.</p>
<ol start="1">
<li>Create a user with necessary permissions as the <code>SYSTEM</code> user:</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> USER sample <span style="color: #993333; font-weight: bold;">IDENTIFIED</span> <span style="color: #993333; font-weight: bold;">BY</span> sample;
<span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">CREATE</span> SESSION<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CREATE</span> ANY CONTEXT<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CREATE</span> ANY PROCEDURE <span style="color: #993333; font-weight: bold;">TO</span> sample;</pre></div></div>

</div>
<ol start="2">
<li>Create the <code>CONTEXT</code> reference as the <code>SAMPLE</code> user, which uses a function to populate the <code>CONTEXT</code>.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> CONTEXT sample_ctx <span style="color: #993333; font-weight: bold;">USING</span> set_context;</pre></div></div>

</div>
<ol start="3">
<li>Create the function as the <code>SAMPLE</code> user to <em>set the context</em>. The <code>CONTEXT</code> is a literal value inside the procedure with a name and value pair.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> PROCEDURE set_context
<span style="color: #66cc66;">&#40;</span> pname  VARCHAR2
<span style="color: #66cc66;">,</span> pvalue VARCHAR2<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
BEGIN
  <span style="color: #808080; font-style: italic;">-- Create a session with a previously defined context.</span>
  DBMS_SESSION<span style="color: #66cc66;">.</span>SET_CONTEXT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SAMPLE_CTX'</span><span style="color: #66cc66;">,</span>pname<span style="color: #66cc66;">,</span>pvalue<span style="color: #66cc66;">&#41;</span>;
END;
<span style="color: #66cc66;">/</span></pre></div></div>

</div>
<ol start="4">
<li>Set the local session <em>sample_ctx</em> <code>CONTEXT</code> as the <code>SAMPLE</code> user.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXECUTE set_context<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'email'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'sherman@atlanta.org'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</div>
<ol start="5">
<li>You now query the user-defined <code>CONTEXT</code> with case insensitive strings that match the <code>CONTEXT</code> and <code>pname</code> call parameter that you set it. The following shows that query against dual. You should note that it returns a case sensitive string of the <code>pvalue</code> call parameter.</li>
</ol>
<div style="padding-left:30px">

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> sys_context<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'sample_ctx'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'email'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> dual;</pre></div></div>

</div>
<p>As always, I hope this helps somebody and saves them time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/05/05/user-defined-sys_context/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When dropping is adding?</title>
		<link>http://blog.mclaughlinsoftware.com/2010/05/04/when-dropping-is-adding/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/05/04/when-dropping-is-adding/#comments</comments>
		<pubDate>Tue, 04 May 2010 23:35:55 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3725</guid>
		<description><![CDATA[I was working through some example files and test scripts with Virtual Private Databases and discovered a nifty and potentially misleading error. Google didn&#8217;t pick up any search results with it, so I thought noting it would be a good idea. When you create a security policy with DBMS_RLS.ADD_POLICY incorrectly, and then try to drop [...]]]></description>
			<content:encoded><![CDATA[<p>I was working through some example files and test scripts with Virtual Private Databases and discovered a nifty and potentially misleading error. Google didn&#8217;t pick up any search results with it, so I thought noting it would be a good idea.</p>
<p>When you create a security policy with <code>DBMS_RLS.ADD_POLICY</code> incorrectly, and then try to drop it, you must make sure to include the <code>OBJECT_SCHEMA</code> parameter. If you don&#8217;t and provide named parameters like the following, you&#8217;ll raise an error.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN
  DBMS_RLS<span style="color: #66cc66;">.</span>DROP_POLICY<span style="color: #66cc66;">&#40;</span>object_name<span style="color: #66cc66;">=&gt;</span><span style="color: #ff0000;">'valid_table'</span>
                      <span style="color: #66cc66;">,</span>policy_name<span style="color: #66cc66;">=&gt;</span><span style="color: #ff0000;">'valid_policy'</span><span style="color: #66cc66;">&#41;</span>;
END;
<span style="color: #66cc66;">/</span></pre></div></div>

<p>The error is quite misleading, as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN
<span style="color: #66cc66;">*</span>
ERROR at line <span style="color: #cc66cc;">1</span>:
ORA<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">28103</span>: adding a policy <span style="color: #993333; font-weight: bold;">TO</span> an object owned <span style="color: #993333; font-weight: bold;">BY</span> SYS <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> allowed
ORA<span style="color: #66cc66;">-</span>06512: at <span style="color: #ff0000;">&quot;SYS.DBMS_RLS&quot;</span><span style="color: #66cc66;">,</span> line <span style="color: #cc66cc;">59</span>
ORA<span style="color: #66cc66;">-</span>06512: at line <span style="color: #cc66cc;">2</span></pre></div></div>

<p>The error is actually triggered when the <code>OBJECT_SCHEMA</code> is required. The default value is a <em>NULL</em> in the <code>DBMS_RLS</code> package specification.</p>
<p>The correct syntax is:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN
  DBMS_RLS<span style="color: #66cc66;">.</span>DROP_POLICY<span style="color: #66cc66;">&#40;</span>object_schema<span style="color: #66cc66;">=&gt;</span><span style="color: #ff0000;">'valid_schema'</span>
                      <span style="color: #66cc66;">,</span>object_name<span style="color: #66cc66;">=&gt;</span><span style="color: #ff0000;">'valid_table'</span>
                      <span style="color: #66cc66;">,</span>policy_name<span style="color: #66cc66;">=&gt;</span><span style="color: #ff0000;">'valid_policy'</span><span style="color: #66cc66;">&#41;</span>;
END;
<span style="color: #66cc66;">/</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/05/04/when-dropping-is-adding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manual Oracle Service</title>
		<link>http://blog.mclaughlinsoftware.com/2010/03/18/manual-oracle-service/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/03/18/manual-oracle-service/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 05:03:19 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Microsoft Vista]]></category>
		<category><![CDATA[Microsoft Windows 7]]></category>
		<category><![CDATA[Oracle]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3649</guid>
		<description><![CDATA[Ruairi asked how you could disable automatic start of the Oracle Service for Oracle 11g on Windows 7 (a comment here). Ruairi also provided a nice Windows shell script that you can copy for starting and stopping the Oracle Service in his last comment. The simplest way is to launch a command shell because I [...]]]></description>
			<content:encoded><![CDATA[<p>Ruairi asked how you could disable automatic start of the Oracle Service for Oracle 11g on Windows 7 (a <a href="http://blog.mclaughlinsoftware.com/2009/11/27/oracle-11g-on-windows-7/">comment here</a>). Ruairi also provided a nice Windows shell script that you can copy for starting and stopping the Oracle Service in his last comment.</p>
<p>The simplest way is to launch a command shell because I don&#8217;t want to provide all the navigation variations for different Windows versions.</p>
<p>Basically, you do that by clicking the Windows Start button and type <code>cmd</code> word in the run entry box. This launches a command session. Type the following from the prompt. It launches the Windows Services console in all relevant versions:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">C:\<span style="color: #000000; font-weight: bold;">&gt;</span> services.msc</pre></div></div>

<p>Now you&#8217;ll see the Windows Services console. Navigate to the Oracle Service and right click on it. You choose <em>Properties</em>.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2010/03/Windows7DisableAutoService1.png"><img src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2010/03/Windows7DisableAutoService1.png" alt="" title="Windows7DisableAutoService1" width="612" height="450" style="border:none" class="aligncenter size-full wp-image-3650" /></a></p>
<p>That will bring you to this screen. Click on the drop down for the <em>Startup type</em> and choose <em>Manual</em>. Click the <em>OK</em> button to complete the step. That&#8217;s it, the next time you start the machine the Oracle database won&#8217;t start automatically. You should do the same to the other Oracle Services.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2010/03/Windows7DisableAutoService2.png"><img src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2010/03/Windows7DisableAutoService2.png" alt="" title="Windows7DisableAutoService2" width="420" height="474" style="border:none" class="aligncenter size-full wp-image-3651" /></a></p>
<p>If you don&#8217;t have a lot of memory and it&#8217;s a development machine, this makes a lot of sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/03/18/manual-oracle-service/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Certified Expert Exam</title>
		<link>http://blog.mclaughlinsoftware.com/2010/03/17/sql-certified-expert-exam/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/03/17/sql-certified-expert-exam/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 03:34:31 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Oracle XE]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3642</guid>
		<description><![CDATA[I&#8217;ve been working with one of my lab tutors to have him take the 1Z0-047 Oracle Database SQL Expert test. He checked out the online practice exam, and found a couple interesting questions and new syntax. At least, it was new to me. Naturally, I checked it out. I&#8217;ve also added it to my online [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with one of my lab tutors to have him take the <a href="http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=41&#038;p_org_id=1001&#038;lang=US&#038;p_exam_id=1Z0_047">1Z0-047 Oracle Database SQL Expert</a> test. He checked out the online practice exam, and found a couple interesting questions and new syntax. At least, it was new to me.</p>
<p>Naturally, I checked it out. I&#8217;ve also added it to my online tutorial for the class. Perhaps I&#8217;m a creature of habit but a range non-equijion is always a filtered cross product logically. Certainly, the explain plans indicate that this new syntax has zero performance change over the other forms.</p>
<p>I once used the comma-delimited tables (like everybody else), but now I try to always use the newer <code>CROSS JOIN</code> syntax. In both cases the range join is put in the <code>WHERE</code> clause. The new syntax uses an <code>INNER JOIN</code> and an <code>ON</code> clause to hold the range match. Examples of all are below.</p>
<p><span style="font-size:110%;font-weight:bold;font-style:italic">Comma-delimited Filtered Cross Join</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>   c<span style="color: #66cc66;">.</span>month_short_name
<span style="color: #66cc66;">,</span>        t<span style="color: #66cc66;">.</span>transaction_amount
<span style="color: #993333; font-weight: bold;">FROM</span>     calendar_join c<span style="color: #66cc66;">,</span> transaction_join t
<span style="color: #993333; font-weight: bold;">WHERE</span>    t<span style="color: #66cc66;">.</span>transaction_date <span style="color: #993333; font-weight: bold;">BETWEEN</span> c<span style="color: #66cc66;">.</span>start_date <span style="color: #993333; font-weight: bold;">AND</span> c<span style="color: #66cc66;">.</span>end_date
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> EXTRACT<span style="color: #66cc66;">&#40;</span>MONTH <span style="color: #993333; font-weight: bold;">FROM</span> t<span style="color: #66cc66;">.</span>transaction_date<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><span style="font-size:110%;font-weight:bold;font-style:italic">Filtered <code>CROSS JOIN</code></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>   c<span style="color: #66cc66;">.</span>month_short_name
<span style="color: #66cc66;">,</span>        t<span style="color: #66cc66;">.</span>transaction_amount
<span style="color: #993333; font-weight: bold;">FROM</span>     calendar_join c <span style="color: #993333; font-weight: bold;">CROSS</span> <span style="color: #993333; font-weight: bold;">JOIN</span> transaction_join t
<span style="color: #993333; font-weight: bold;">WHERE</span>    t<span style="color: #66cc66;">.</span>transaction_date <span style="color: #993333; font-weight: bold;">BETWEEN</span> c<span style="color: #66cc66;">.</span>start_date <span style="color: #993333; font-weight: bold;">AND</span> c<span style="color: #66cc66;">.</span>end_date
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> EXTRACT<span style="color: #66cc66;">&#40;</span>MONTH <span style="color: #993333; font-weight: bold;">FROM</span> t<span style="color: #66cc66;">.</span>transaction_date<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><span style="font-size:110%;font-weight:bold;font-style:italic">Range filtered <code>INNER JOIN</code></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>   c<span style="color: #66cc66;">.</span>month_short_name
<span style="color: #66cc66;">,</span>        t<span style="color: #66cc66;">.</span>transaction_amount
<span style="color: #993333; font-weight: bold;">FROM</span>     calendar_join c <span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> transaction_join t
<span style="color: #993333; font-weight: bold;">ON</span>      <span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">.</span>transaction_date <span style="color: #993333; font-weight: bold;">BETWEEN</span> c<span style="color: #66cc66;">.</span>start_date <span style="color: #993333; font-weight: bold;">AND</span> c<span style="color: #66cc66;">.</span>end_date<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> EXTRACT<span style="color: #66cc66;">&#40;</span>MONTH <span style="color: #993333; font-weight: bold;">FROM</span> t<span style="color: #66cc66;">.</span>transaction_date<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Without an <code>INDEX</code> on the start and end date of the <code>CALENDAR_JOIN</code> table, the Oracle explain plan for all three queries is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">Query Plan
<span style="color: #808080; font-style: italic;">----------------------------------------------</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> STATEMENT   Cost <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">9</span>
  SORT <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
    MERGE <span style="color: #993333; font-weight: bold;">JOIN</span>
      SORT <span style="color: #993333; font-weight: bold;">JOIN</span>
        <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS <span style="color: #993333; font-weight: bold;">FULL</span> TRANSACTION_JOIN
      FILTER
        SORT <span style="color: #993333; font-weight: bold;">JOIN</span>
          <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS <span style="color: #993333; font-weight: bold;">FULL</span> CALENDAR_JOIN</pre></td></tr></table></div>

<p>Naturally, an <code>INDEX</code> on the <code>START_DATE</code> and <code>END_DATE</code> columns improves performance. The results again for all three are the same.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">Query Plan
<span style="color: #808080; font-style: italic;">----------------------------------------------</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> STATEMENT   Cost <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">6</span>
  SORT <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
    <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">INDEX</span> ROWID CALENDAR_JOIN
      NESTED LOOPS
        <span style="color: #993333; font-weight: bold;">TABLE</span> ACCESS <span style="color: #993333; font-weight: bold;">FULL</span> TRANSACTION_JOIN
        <span style="color: #993333; font-weight: bold;">INDEX</span> RANGE SCAN DATE_RANGE</pre></td></tr></table></div>

<p>Unless I&#8217;m missing something, it looks like its only a matter of style. However, make sure you know that new one because it appears that it&#8217;s on the OCP exam. <img src='http://blog.mclaughlinsoftware.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Comments are always welcome &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/03/17/sql-certified-expert-exam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle Trigger on Merge</title>
		<link>http://blog.mclaughlinsoftware.com/2010/03/16/oracle-trigger-on-merge/</link>
		<comments>http://blog.mclaughlinsoftware.com/2010/03/16/oracle-trigger-on-merge/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 04:40:25 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.mclaughlinsoftware.com/?p=3638</guid>
		<description><![CDATA[An interesting question came up today while discussing PL/SQL database triggers. Could you create a trigger on a MERGE statement, like this: 1 2 3 4 5 6 7 8 CREATE OR REPLACE TRIGGER contact_merge_t1 BEFORE MERGE OF last_name ON contact_merge FOR EACH ROW WHEN &#40;REGEXP_LIKE&#40;new.last_name,' '&#41;&#41; BEGIN :new.last_name := REGEXP_REPLACE&#40;:new.last_name,' ','-',1,1&#41;; END contact_merge_t1; / [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting question came up today while discussing PL/SQL database triggers. Could you create a trigger on a <code>MERGE</code> statement, like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> contact_merge_t1
BEFORE MERGE OF last_name <span style="color: #993333; font-weight: bold;">ON</span> contact_merge
<span style="color: #993333; font-weight: bold;">FOR</span> EACH ROW
WHEN <span style="color: #66cc66;">&#40;</span>REGEXP_LIKE<span style="color: #66cc66;">&#40;</span>new<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
BEGIN
  :new<span style="color: #66cc66;">.</span>last_name :<span style="color: #66cc66;">=</span> REGEXP_REPLACE<span style="color: #66cc66;">&#40;</span>:new<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
END contact_merge_t1;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

<p>The answer is, no you can&#8217;t. It&#8217;ll raise an <code>ORA-04073</code> error if you attempt it, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEFORE MERGE OF last_name <span style="color: #993333; font-weight: bold;">ON</span> contact
             <span style="color: #66cc66;">*</span>
ERROR at line <span style="color: #cc66cc;">2</span>:
ORA<span style="color: #66cc66;">-</span>04073: <span style="color: #993333; font-weight: bold;">COLUMN</span> list <span style="color: #993333; font-weight: bold;">NOT</span> valid <span style="color: #993333; font-weight: bold;">FOR</span> this <span style="color: #993333; font-weight: bold;">TRIGGER</span> type</pre></div></div>

<p>The only supported DML events are <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code>. The following DML trigger works against a <code>MERGE</code> statement. After all a <code>MERGE</code> statement is nothing more than an <code>INSERT</code> or <code>UPDATE</code> statement.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> contact_merge_t1
BEFORE <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> OF last_name <span style="color: #993333; font-weight: bold;">ON</span> contact_merge
<span style="color: #993333; font-weight: bold;">FOR</span> EACH ROW
WHEN <span style="color: #66cc66;">&#40;</span>REGEXP_LIKE<span style="color: #66cc66;">&#40;</span>new<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
BEGIN
  :new<span style="color: #66cc66;">.</span>last_name :<span style="color: #66cc66;">=</span> REGEXP_REPLACE<span style="color: #66cc66;">&#40;</span>:new<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
END contact_merge_t1;
<span style="color: #66cc66;">/</span></pre></td></tr></table></div>

<div class="dropdownbox" style="padding-left:20px;background:#FFFFFF">
<p title="Click to see content ..." class="dropdownclick"><strong><em><span style="font-size:125%">Complete Code Sample</span></em></strong> <span>&darr;</span></p>
<p style="clear: both">
Expand this section to see the sample working code.</p>
<div>
<p>This script creates a <code>CONTACT</code> table, a row-level <code>TRIGGER</code>, a <code>MERGE</code> statement, and query to display the results.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Conditionally drop the table.</span>
BEGIN
  <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">FROM</span> user_tables <span style="color: #993333; font-weight: bold;">WHERE</span> table_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'CONTACT_MERGE'</span><span style="color: #66cc66;">&#41;</span> LOOP
    EXECUTE IMMEDIATE <span style="color: #ff0000;">'DROP TABLE contact_merge'</span>;
  END LOOP;
END;
<span style="color: #66cc66;">/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Create the table.</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> contact_merge
<span style="color: #66cc66;">&#40;</span> contact_id                  NUMBER
<span style="color: #66cc66;">,</span> member_id                   NUMBER       <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> contact_type                NUMBER       <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> first_name                  VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> middle_name                 VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> last_name                   VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> created_by                  NUMBER       <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> creation_date               DATE         <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> last_updated_by             NUMBER       <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span> last_update_date            DATE
<span style="color: #66cc66;">,</span> CONSTRAINT contact_merge_pk <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>contact_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Create the trigger to enforce hyphenated last names..</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> contact_merge_t1
BEFORE <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> OF last_name <span style="color: #993333; font-weight: bold;">ON</span> contact_merge
<span style="color: #993333; font-weight: bold;">FOR</span> EACH ROW
WHEN <span style="color: #66cc66;">&#40;</span>REGEXP_LIKE<span style="color: #66cc66;">&#40;</span>new<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
BEGIN
  :new<span style="color: #66cc66;">.</span>last_name :<span style="color: #66cc66;">=</span> REGEXP_REPLACE<span style="color: #66cc66;">&#40;</span>:new<span style="color: #66cc66;">.</span>last_name<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
END contact_merge_t1;
<span style="color: #66cc66;">/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Merge statement that violates business rule.</span>
MERGE <span style="color: #993333; font-weight: bold;">INTO</span> contact_merge target
<span style="color: #993333; font-weight: bold;">USING</span>
<span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">2001</span> <span style="color: #993333; font-weight: bold;">AS</span> contact_id
  <span style="color: #66cc66;">,</span>      <span style="color: #cc66cc;">1001</span> <span style="color: #993333; font-weight: bold;">AS</span> member_id
  <span style="color: #66cc66;">,</span>      <span style="color: #cc66cc;">1001</span> <span style="color: #993333; font-weight: bold;">AS</span> contact_type
  <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Catherine'</span> <span style="color: #993333; font-weight: bold;">AS</span> first_name
  <span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">AS</span> middle_name
  <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Zeta Jones'</span> <span style="color: #993333; font-weight: bold;">AS</span> last_name
  <span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">AS</span> created_by
  <span style="color: #66cc66;">,</span> SYSDATE <span style="color: #993333; font-weight: bold;">AS</span> creation_date
  <span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">AS</span> last_updated_by
  <span style="color: #66cc66;">,</span> SYSDATE <span style="color: #993333; font-weight: bold;">AS</span> last_update_date
<span style="color: #993333; font-weight: bold;">FROM</span> dual<span style="color: #66cc66;">&#41;</span> source
<span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#40;</span>target<span style="color: #66cc66;">.</span>contact_id <span style="color: #66cc66;">=</span> source<span style="color: #66cc66;">.</span>contact_id<span style="color: #66cc66;">&#41;</span>
WHEN MATCHED THEN
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #993333; font-weight: bold;">SET</span> target<span style="color: #66cc66;">.</span>last_updated_by <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span>
WHEN <span style="color: #993333; font-weight: bold;">NOT</span> MATCHED THEN
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">VALUES</span>
<span style="color: #66cc66;">&#40;</span> source<span style="color: #66cc66;">.</span>contact_id
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>member_id
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>contact_type
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>first_name
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>middle_name
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>last_name
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>created_by
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>creation_date
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>last_updated_by
<span style="color: #66cc66;">,</span> source<span style="color: #66cc66;">.</span>last_update_date <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Query results.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> first_name<span style="color: #66cc66;">||</span>DECODE<span style="color: #66cc66;">&#40;</span>middle_name<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">||</span>middle_name<span style="color: #66cc66;">||</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">||</span>last_name <span style="color: #993333; font-weight: bold;">AS</span> full_name
<span style="color: #993333; font-weight: bold;">FROM</span>   contact_merge
<span style="color: #993333; font-weight: bold;">WHERE</span>  first_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Catherine'</span>;</pre></td></tr></table></div>

</div>
</div>
<p style="clear:both">
]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2010/03/16/oracle-trigger-on-merge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
