<?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>DJS Consulting Tech Blog &#187; SQL</title>
	<atom:link href="http://techblog.djs-consulting.com/category/programming/sql/feed" rel="self" type="application/rss+xml" />
	<link>http://techblog.djs-consulting.com</link>
	<description>Technical Information You Can Use</description>
	<lastBuildDate>Mon, 02 Jan 2012 18:44:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Oracle SQL Developer Debian Package</title>
		<link>http://techblog.djs-consulting.com/2008/oracle-sql-developer-debian-package.html</link>
		<comments>http://techblog.djs-consulting.com/2008/oracle-sql-developer-debian-package.html#comments</comments>
		<pubDate>Wed, 29 Oct 2008 13:17:14 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[alien]]></category>
		<category><![CDATA[deb]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[rpm]]></category>
		<category><![CDATA[sql developer]]></category>

		<guid isPermaLink="false">http://techblog.johnson/?p=167</guid>
		<description><![CDATA[Oracle SQL Developer is a Java-based tool that provides a graphical interface to a database. While it&#8217;s main focus is Oracle (of course), it can be hooked up, via JDBC, to many other databases, such as MySQL, PostgreSQL, and SQL &#8230; <a href="http://techblog.djs-consulting.com/2008/oracle-sql-developer-debian-package.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a title="Oracle SQL Developer • Oracle" href="http://www.oracle.com/technology/products/database/sql_developer/index.html">Oracle SQL Developer</a> is a Java-based tool that provides a graphical interface to a database. While it&#8217;s main focus is Oracle (of course), it can be hooked up, via JDBC, to many other databases, such as MySQL, PostgreSQL, and SQL Server. It&#8217;s similar to <a href="http://www.toadsoft.com/">Toad</a>, but is provided by Oracle at no cost.</p>
<p>Oracle provides SQL Developer in either an RPM, or a generic binary install. I like the ability to manage packages, but I&#8217;ve never had much luck at getting RPM to run on Ubuntu. I downloaded the RPM file, and, using <a href="http://kitenet.net/~joey/code/alien/">alien</a>, I converted the package to a .deb package (Debian package format) and installed it. It worked like a charm!</p>
<p>I haven&#8217;t tested it with gcj, but using Sun&#8217;s Java 6 update 7 from the Ubuntu repositories, it ran just fine. After you install the package, do a directory list on /usr/lib/jvm. You&#8217;re looking for the Sun JDK &#8211; if it&#8217;s installed, you&#8217;ll have a symlink java-6-sun that points to java-6-sun-1.6.0.07. Once you&#8217;ve determined the location of the JDK, run &#8220;sqldeveloper&#8221; from the command line &#8211; the program will prompt you for the path to your JDK. Enter it (probably &#8220;/usr/lib/jvm/java-6-sun&#8221;) and you&#8217;re good to go. (You have to install the package as root &#8211; but, for the rest of these steps, use your normal user, not root, as this puts settings in a .sqldeveloper directory off your home directory.) The package installs an icon in the &#8220;Programming&#8221; or &#8220;Development&#8221; group. Once you&#8217;ve told it where the JDK is, you can use this to launch it.</p>
<p><a title="SQL Developer 1.5.1 Debian Package • DJS Consulting Linux Software Repository" href="http://djs-consulting.com/linux/software/sqldeveloper/sqldeveloper_1.5.54.40-2_all.deb">Download SQL Developer 1.5.1 Debian Package</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.djs-consulting.com/2008/oracle-sql-developer-debian-package.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Algorithm for One-to-Many Child Table Updates</title>
		<link>http://techblog.djs-consulting.com/2008/algorithm-for-one-to-many-child-table-updates.html</link>
		<comments>http://techblog.djs-consulting.com/2008/algorithm-for-one-to-many-child-table-updates.html#comments</comments>
		<pubDate>Sat, 29 Mar 2008 02:13:24 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[nsx]]></category>
		<category><![CDATA[one-to-many]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://techblog.johnson/?p=150</guid>
		<description><![CDATA[While working on the Not So Extreme Makeover: Community Edition site, I came up with an algorithm that simplifies anything else I&#8217;ve ever written to deal with this condition. I&#8217;ll set the scenario, explain the algorithm, share how I implemented &#8230; <a href="http://techblog.djs-consulting.com/2008/algorithm-for-one-to-many-child-table-updates.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While working on the <a href="http://notsoextreme.org" title="Not So Extreme Makeover: Community Edition">Not So Extreme Makeover: Community Edition</a> site, I came up with an algorithm that simplifies anything else I&#8217;ve ever written to deal with this condition.  I&#8217;ll set the scenario, explain the algorithm, share how I implemented it in PHP, and provide a modification if the scenario is a bit more complicated.</p>
<p><strong>Scenario</strong> &#8211; You have two parent tables, and a child table with a many-to-one relationship with both parent tables, used to map entries in the two parent tables to each other.  For this example, we&#8217;ll use these three tables&#8230;</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
</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;">TABLE</span> volunteer <span style="color: #66cc66;">&#40;</span>
    vol_id  <span style="color: #993333; font-weight: bold;">INTEGER</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    vol_last_name  <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</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>
    <span style="color: #66cc66;">...</span>etc<span style="color: #66cc66;">...</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>vol_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> r_volunteer_area <span style="color: #66cc66;">&#40;</span>
    rva_id  <span style="color: #993333; font-weight: bold;">INTEGER</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    rva_description  <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</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>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>rva_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> volunteer_area <span style="color: #66cc66;">&#40;</span>
    va_volunteer_id  <span style="color: #993333; font-weight: bold;">INTEGER</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    va_area_id  <span style="color: #993333; font-weight: bold;">INTEGER</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>va_volunteer_id<span style="color: #66cc66;">,</span> va_area_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>va_volunteer_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> volunteer <span style="color: #66cc66;">&#40;</span>vol_id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>va_area_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> r_volunteer_area <span style="color: #66cc66;">&#40;</span>rva_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Algorithm</strong> &#8211; The three-step algorithm is as follows&#8230;</p>
<ol>
<li>Create a comma-delimited string of IDs for the child table.</li>
<li>Delete the IDs from the child table that are not in the list.</li>
<li>Insert the IDs into the child table that are not there already.</li>
</ol>
<p><strong>Implementation</strong> &#8211; In PHP, if you have an array, it&#8217;s easy to come up with comma-delimited list.  To get an array of values back in a post, define your fields with &#8220;[]&#8221; after the name&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;checkbox&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;area[]&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;chkArea1&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;chkArea1&quot;</span><span style="color: #339933;">&gt;</span>Do Something<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;checkbox&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;area[]&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;chkArea7&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;7&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>label <span style="color: #b1b100;">for</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;chkArea7&quot;</span><span style="color: #339933;">&gt;</span>Do Something Else<span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Here&#8217;s the PHP code, using <a href="http://us.php.net/pdo" title="PHP Data Objects (PDO)">PHP Data Objects (PDO)</a> as the database interface, behind a helper class that creates the statement, appends the parameters, and executes it.  <em>(The &#8220;quoting&#8221; escapes the statement to avoid potential SQL injection attacks &#8211; putting it in its own class would make the implementation here much cleaner.)</em></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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * STEP 1
 *    Create a comma-delimited list of IDs.
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Quote will return the string as '2,3,4' - since we're using this</span>
<span style="color: #666666; font-style: italic;">// as an IN clause of integers, we'll strip the quotes off.</span>
<span style="color: #000088;">$sAreas</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sAreas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAreas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAreas</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Quote the volunteer ID.</span>
<span style="color: #000088;">$iVol</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pdo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quote</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;vol&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> PDO<span style="color: #339933;">::</span><span style="color: #004000;">PARAM_INT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * STEP 2
 *    Delete the IDs that are no longer in the list.
 */</span>
<span style="color: #000088;">$dbService</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeCommand</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;DELETE FROM volunteer_area
    WHERE   va_volunteer_id = ?
        AND va_area_id NOT IN (<span style="color: #006699; font-weight: bold;">$sAreas</span>)&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iVol</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * STEP 3
 *    Insert the IDs that are not yet in the list.
 */</span>
<span style="color: #000088;">$dbService</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeCommand</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;INSERT INTO volunteer_area
        SELECT <span style="color: #006699; font-weight: bold;">$iVol</span>, rva_id
        FROM r_volunteer_area
        WHERE   rva_id IN (<span style="color: #006699; font-weight: bold;">$sAreas</span>)
            AND rva_id NOT IN
            (SELECT va_area_id
&nbsp;
            FROM volunteer_area
            WHERE va_volunteer_id = ?)&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iVol</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Modification</strong> &#8211; Suppose that now you accepted comments along with each of the checkboxes, so a simple two-integer insert/delete is no longer sufficient.  You would still only need to break step 3 into two steps.</p>
<ol>
<li>Get a list of IDs to update.</li>
<li>For each ID in the posted list
<ol>
<li>If the ID exists in the update list, update it.</li>
<li>Otherwise, insert it.</li>
</ol>
</li>
</ol>
<p>The implementation would then be able to use this list to make the decision without hitting the database every time.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Assume this returns an associative array of IDs.</span>
<span style="color: #000088;">$aUpdates</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbService</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">performSelect</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;SELECT va_area_id
    FROM volunteer_area
    WHERE   va_volunteer_id = ?
        AND va_area_id IN (<span style="color: #006699; font-weight: bold;">$sAreas</span>)&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iVol</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;area&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$iArea</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iArea</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aUpdates</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Update the table</span>
        <span style="color: #339933;">...</span>etc<span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Insert into the table</span>
        <span style="color: #339933;">...</span>etc<span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I think you&#8217;ll agree that this is much better than spinning through a loop, doing a count on each ID to see if it exists, then either doing an update or an insert based on the count.  And, while the implementation here is PHP, it could easily be implemented in any language that supports arrays and database access.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.djs-consulting.com/2008/algorithm-for-one-to-many-child-table-updates.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transferring CLOBs Across Linked Oracle Databases</title>
		<link>http://techblog.djs-consulting.com/2007/transferring-clobs-across-linked-oracle-databases.html</link>
		<comments>http://techblog.djs-consulting.com/2007/transferring-clobs-across-linked-oracle-databases.html#comments</comments>
		<pubDate>Fri, 15 Jun 2007 20:04:02 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[clob]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[linked database]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://techblog.johnson/?p=90</guid>
		<description><![CDATA[Linking databases in Oracle make it easy to share data, and can be useful for replication. However, there is a limitation in Oracle that prevents Character Large Objects (CLOBs) from coming across these links. The following technique uses stored procedures &#8230; <a href="http://techblog.djs-consulting.com/2007/transferring-clobs-across-linked-oracle-databases.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Linking databases in Oracle make it easy to share data, and can be useful for replication. However, there is a limitation in Oracle that prevents Character Large Objects (CLOBs) from coming across these links. The following technique uses stored procedures and a temporary table to pull CLOBs across a database link.</p>
<p>First, you&#8217;ll need the temporary table, which will hold a sequence number, the primary key for the table where you&#8217;ll want to reconstruct the CLOB, and some text. This table can reside in the source or destination database, but must be linked from the other one. For our purposes, it looks like this&#8230;</p>

<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;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> clob_xfer_area
<span style="color: #66cc66;">&#40;</span>
  cxa_pk      <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  cxa_number  <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  cxa_text    varchar2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span> byte<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> clob_xfer_area <span style="color: #993333; font-weight: bold;">ADD</span>
<span style="color: #66cc66;">&#40;</span>
  <span style="color: #993333; font-weight: bold;">CONSTRAINT</span> pk_cxa_id
    <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>cxa_pk<span style="color: #66cc66;">,</span> cxa_number<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Second, you&#8217;ll need the procedure in the source database that breaks the CLOB apart and populates the temporary table.</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
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> serveroutput <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">SIZE</span> <span style="color: #cc66cc;">1000000</span>
<span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">LINES</span> <span style="color: #cc66cc;">1000</span>
<span style="color: #993333; font-weight: bold;">SET</span> pages <span style="color: #cc66cc;">0</span>
<span style="color: #993333; font-weight: bold;">SET</span> tab off
<span style="color: #993333; font-weight: bold;">SET</span> feedback <span style="color: #993333; font-weight: bold;">ON</span>
&nbsp;
<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;">PROCEDURE</span> break_clobs_apart
<span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  v_line_number   <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
  v_text_piece    varchar2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>;
  v_total_length  <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  cursor clob_cur <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> twc_pk<span style="color: #66cc66;">,</span> twc_clob_field
    <span style="color: #993333; font-weight: bold;">FROM</span>   table_with_clob;
&nbsp;
<span style="color: #993333; font-weight: bold;">BEGIN</span> <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">FOR</span> clob_rec <span style="color: #993333; font-weight: bold;">IN</span> clob_cur loop <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
    v_total_length :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
    v_line_number  :<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;
&nbsp;
    while <span style="color: #66cc66;">&#40;</span>v_total_length <span style="color: #66cc66;">&lt;=</span>
           DBMS_LOB<span style="color: #66cc66;">.</span>GETLENGTH<span style="color: #66cc66;">&#40;</span>clob_rec<span style="color: #66cc66;">.</span>twc_clob_field<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> loop <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
      v_line_number :<span style="color: #66cc66;">=</span> v_line_number <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;
      v_text_piece :<span style="color: #66cc66;">=</span> DBMS_LOB<span style="color: #66cc66;">.</span>SUBSTR<span style="color: #66cc66;">&#40;</span>clob_rec<span style="color: #66cc66;">.</span>twc_clob_field<span style="color: #66cc66;">,</span>
        <span style="color: #cc66cc;">3999</span><span style="color: #66cc66;">,</span> v_total_length<span style="color: #66cc66;">&#41;</span>;
      v_total_length :<span style="color: #66cc66;">=</span> v_total_length <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">3999</span>;
&nbsp;
      <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> clob_xfer_area <span style="color: #66cc66;">&#40;</span>
        cxa_pk<span style="color: #66cc66;">,</span>
        cxa_number<span style="color: #66cc66;">,</span>
        cxa_text
      <span style="color: #66cc66;">&#41;</span>
        <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>
          clob_rec<span style="color: #66cc66;">.</span>twc_pk<span style="color: #66cc66;">,</span> <span style="color: #808080; font-style: italic;">-- cxa_pk</span>
          v_line_number<span style="color: #66cc66;">,</span>   <span style="color: #808080; font-style: italic;">-- cxa_number</span>
          v_text_piece     <span style="color: #808080; font-style: italic;">-- cxa_text</span>
        <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #993333; font-weight: bold;">END</span> loop; <span style="color: #808080; font-style: italic;">/* } of while */</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">END</span> loop; <span style="color: #808080; font-style: italic;">/* } of clob_cur */</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">END</span>; <span style="color: #808080; font-style: italic;">/* } of procedure break_clobs_apart */</span></pre></td></tr></table></div>

<p>Third, you&#8217;ll need a procedure in the destination database that puts the CLOB back together, and deletes the data from the temporary table.</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
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> serveroutput <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">SIZE</span> <span style="color: #cc66cc;">1000000</span>
<span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">LINES</span> <span style="color: #cc66cc;">1000</span>
<span style="color: #993333; font-weight: bold;">SET</span> pages <span style="color: #cc66cc;">0</span>
<span style="color: #993333; font-weight: bold;">SET</span> feedback <span style="color: #993333; font-weight: bold;">ON</span>
<span style="color: #993333; font-weight: bold;">SET</span> tab off
&nbsp;
<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;">PROCEDURE</span> put_clobs_together
<span style="color: #993333; font-weight: bold;">IS</span>
  v_new_clob   <span style="color: #993333; font-weight: bold;">CLOB</span>;
&nbsp;
  cursor pk_cur <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span> cxa_pk
    <span style="color: #993333; font-weight: bold;">FROM</span>   clob_xfer_area;
&nbsp;
  cursor piece_cur<span style="color: #66cc66;">&#40;</span>p_cxa_pk <span style="color: #993333; font-weight: bold;">NUMBER</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> cxa_text
    <span style="color: #993333; font-weight: bold;">FROM</span>   clob_xfer_area
    <span style="color: #993333; font-weight: bold;">WHERE</span>  cxa_pk <span style="color: #66cc66;">=</span> p_cxa_pk
    <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> cxa_number;
&nbsp;
<span style="color: #993333; font-weight: bold;">BEGIN</span> <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">FOR</span> pk_rec <span style="color: #993333; font-weight: bold;">IN</span> pk_cur loop <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
    DBMS_LOB<span style="color: #66cc66;">.</span>CREATETEMPORARY<span style="color: #66cc66;">&#40;</span>v_new_clob<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">TRUE</span><span style="color: #66cc66;">&#41;</span>;
    DBMS_LOB<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">OPEN</span><span style="color: #66cc66;">&#40;</span>v_new_clob<span style="color: #66cc66;">,</span> DBMS_LOB<span style="color: #66cc66;">.</span>LOB_READWRITE<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #993333; font-weight: bold;">FOR</span> piece_rec <span style="color: #993333; font-weight: bold;">IN</span> piece_cur<span style="color: #66cc66;">&#40;</span>pk_rec<span style="color: #66cc66;">.</span>cxa_pk<span style="color: #66cc66;">&#41;</span> loop <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
      DBMS_LOB<span style="color: #66cc66;">.</span>WRITEAPPEND<span style="color: #66cc66;">&#40;</span>v_new_clob<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>piece_rec<span style="color: #66cc66;">.</span>cxa_text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
        piece_rec<span style="color: #66cc66;">.</span>cxa_text<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #993333; font-weight: bold;">END</span> loop;  <span style="color: #808080; font-style: italic;">/* } of piece_cur */</span>
&nbsp;
    DBMS_LOB<span style="color: #66cc66;">.</span>CLOSE<span style="color: #66cc66;">&#40;</span>v_new_clob<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #993333; font-weight: bold;">UPDATE</span> dest_table_with_clob
      <span style="color: #993333; font-weight: bold;">SET</span>  migrated_clob <span style="color: #66cc66;">=</span> v_new_clob
      <span style="color: #993333; font-weight: bold;">WHERE</span> dtwc_pk <span style="color: #66cc66;">=</span> pk_rec<span style="color: #66cc66;">.</span>cxa_pk;
&nbsp;
  <span style="color: #993333; font-weight: bold;">END</span> loop; <span style="color: #808080; font-style: italic;">/* } of pk_cur */</span>
&nbsp;
  <span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> clob_xfer_area;
&nbsp;
<span style="color: #993333; font-weight: bold;">END</span>; <span style="color: #808080; font-style: italic;">/* } of procedure put_clobs_together */</span></pre></td></tr></table></div>

<p>Finally, you&#8217;ll need a procedure that controls the whole thing. We&#8217;ll assume that this procedure is loaded in the destination database, and the source database is linked with the name &#8220;source&#8221;.</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
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">LINES</span> <span style="color: #cc66cc;">1000</span>
<span style="color: #993333; font-weight: bold;">SET</span> pages <span style="color: #cc66cc;">0</span>
<span style="color: #993333; font-weight: bold;">SET</span> feedback <span style="color: #993333; font-weight: bold;">ON</span>
<span style="color: #993333; font-weight: bold;">SET</span> tab off
&nbsp;
<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;">PROCEDURE</span> xfer_clobs
<span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">BEGIN</span> <span style="color: #808080; font-style: italic;">/* { */</span>
&nbsp;
  break_clobs_apart@<span style="color: #993333; font-weight: bold;">SOURCE</span>;
  put_clobs_together;
&nbsp;
<span style="color: #993333; font-weight: bold;">END</span>; <span style="color: #808080; font-style: italic;">/* } */</span></pre></td></tr></table></div>

<p>(This does not include a commit &#8211; the changes will not be persistent unless they are committed.)</p>
<p>Of course, these processes could (and, to be useful, likely would) be integrated into other procedures and scripts. But, this framework will successfully transfer CLOBs across linked databases in Oracle.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.djs-consulting.com/2007/transferring-clobs-across-linked-oracle-databases.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

