<?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; PL/SQL</title>
	<atom:link href="http://djs-consulting.com/linux/blog/category/programming/sql/plsql/feed" rel="self" type="application/rss+xml" />
	<link>http://djs-consulting.com/linux/blog</link>
	<description>Technical Information You Can Use (or not - but hey, at least it&#039;s free!)</description>
	<lastBuildDate>Tue, 02 Mar 2010 19:11:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Transferring CLOBs Across Linked Oracle Databases</title>
		<link>http://djs-consulting.com/linux/blog/2007/transferring-clobs-across-linked-oracle-databases</link>
		<comments>http://djs-consulting.com/linux/blog/2007/transferring-clobs-across-linked-oracle-databases#comments</comments>
		<pubDate>Fri, 15 Jun 2007 20:04:09 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>

		<guid isPermaLink="false">http://www.djs-consulting.com/linux/blog/2007/transferring-clobs-across-linked-oracle-databases</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 and a temporary table to pull CLOBs across a database link.
First, you&#8217;ll need [...]]]></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="plsql plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">TABLE</span> clob_xfer_area
<span style="color: #00F;">&#40;</span>
  cxa_pk      <span style="color: #00F;">NUMBER</span><span style="color: #00F;">&#40;</span><span style="color: #800;">12</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
  cxa_number  <span style="color: #00F;">NUMBER</span><span style="color: #00F;">&#40;</span><span style="color: #800;">12</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
  cxa_text    <span style="color: #00F;">VARCHAR2</span><span style="color: #00F;">&#40;</span><span style="color: #800;">4000</span> byte<span style="color: #00F;">&#41;</span>
<span style="color: #00F;">&#41;</span>;
<span style="color: #00F;">ALTER</span> <span style="color: #00F;">TABLE</span> clob_xfer_area add
<span style="color: #00F;">&#40;</span>
  constraint pk_cxa_id
    primary key <span style="color: #00F;">&#40;</span>cxa_pk<span style="color: #00F;">,</span> cxa_number<span style="color: #00F;">&#41;</span>
<span style="color: #00F;">&#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="plsql plsql" style="font-family:monospace;"><span style="color: #00F;">SET</span> serveroutput <span style="color: #00F;">ON</span> size <span style="color: #800;">1000000</span>
<span style="color: #00F;">SET</span> lines <span style="color: #800;">1000</span>
<span style="color: #00F;">SET</span> pages 0
<span style="color: #00F;">SET</span> tab off
<span style="color: #00F;">SET</span> feedback <span style="color: #00F;">ON</span>
&nbsp;
<span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span>
<span style="color: #00F;">PROCEDURE</span> break_clobs_apart
<span style="color: #00F;">IS</span>
&nbsp;
  v_line_number   <span style="color: #00F;">NUMBER</span><span style="color: #00F;">&#40;</span><span style="color: #800;">3</span><span style="color: #00F;">&#41;</span>;
  v_text_piece    <span style="color: #00F;">VARCHAR2</span><span style="color: #00F;">&#40;</span><span style="color: #800;">4000</span><span style="color: #00F;">&#41;</span>;
  v_total_length  <span style="color: #00F;">NUMBER</span><span style="color: #00F;">&#40;</span><span style="color: #800;">12</span><span style="color: #00F;">&#41;</span>;
&nbsp;
  <span style="color: #00F;">CURSOR</span> clob_cur <span style="color: #00F;">IS</span>
    <span style="color: #00F;">SELECT</span> twc_pk<span style="color: #00F;">,</span> twc_clob_field
    <span style="color: #00F;">FROM</span>   table_with_clob;
&nbsp;
<span style="color: #00F;">BEGIN</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
  <span style="color: #00F;">FOR</span> clob_rec <span style="color: #00F;">IN</span> clob_cur <span style="color: #00F;">LOOP</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
    v_total_length <span style="color: #00F;">:=</span> <span style="color: #800;">1</span>;
    v_line_number  <span style="color: #00F;">:=</span> 0;
&nbsp;
    <span style="color: #00F;">WHILE</span> <span style="color: #00F;">&#40;</span>v_total_length <span style="color: #00F;">&lt;=</span>
           <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span>GETLENGTH<span style="color: #00F;">&#40;</span>clob_rec<span style="color: #00F;">.</span>twc_clob_field<span style="color: #00F;">&#41;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">LOOP</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
      v_line_number <span style="color: #00F;">:=</span> v_line_number <span style="color: #00F;">+</span> <span style="color: #800;">1</span>;
      v_text_piece <span style="color: #00F;">:=</span> <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span><span style="color: #000;">SUBSTR</span><span style="color: #00F;">&#40;</span>clob_rec<span style="color: #00F;">.</span>twc_clob_field<span style="color: #00F;">,</span>
        <span style="color: #800;">3999</span><span style="color: #00F;">,</span> v_total_length<span style="color: #00F;">&#41;</span>;
      v_total_length <span style="color: #00F;">:=</span> v_total_length <span style="color: #00F;">+</span> <span style="color: #800;">3999</span>;
&nbsp;
      <span style="color: #00F;">INSERT</span> <span style="color: #00F;">INTO</span> clob_xfer_area <span style="color: #00F;">&#40;</span>
        cxa_pk<span style="color: #00F;">,</span>
        cxa_number<span style="color: #00F;">,</span>
        cxa_text
      <span style="color: #00F;">&#41;</span>
        <span style="color: #00F;">VALUES</span> <span style="color: #00F;">&#40;</span>
          clob_rec<span style="color: #00F;">.</span>twc_pk<span style="color: #00F;">,</span> <span style="color: #080; font-style: italic;">-- cxa_pk</span>
          v_line_number<span style="color: #00F;">,</span>   <span style="color: #080; font-style: italic;">-- cxa_number</span>
          v_text_piece     <span style="color: #080; font-style: italic;">-- cxa_text</span>
        <span style="color: #00F;">&#41;</span>;
&nbsp;
    <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span>; <span style="color: #080; font-style: italic;">/* } of while */</span>
&nbsp;
  <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span>; <span style="color: #080; font-style: italic;">/* } of clob_cur */</span>
&nbsp;
<span style="color: #00F;">END</span>; <span style="color: #080; 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="plsql plsql" style="font-family:monospace;"><span style="color: #00F;">SET</span> serveroutput <span style="color: #00F;">ON</span> size <span style="color: #800;">1000000</span>
<span style="color: #00F;">SET</span> lines <span style="color: #800;">1000</span>
<span style="color: #00F;">SET</span> pages 0
<span style="color: #00F;">SET</span> feedback <span style="color: #00F;">ON</span>
<span style="color: #00F;">SET</span> tab off
&nbsp;
<span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span>
<span style="color: #00F;">PROCEDURE</span> put_clobs_together
<span style="color: #00F;">IS</span>
  v_new_clob   clob;
&nbsp;
  <span style="color: #00F;">CURSOR</span> pk_cur <span style="color: #00F;">IS</span>
    <span style="color: #00F;">SELECT</span> <span style="color: #00F;">DISTINCT</span> cxa_pk
    <span style="color: #00F;">FROM</span>   clob_xfer_area;
&nbsp;
  <span style="color: #00F;">CURSOR</span> piece_cur<span style="color: #00F;">&#40;</span>p_cxa_pk <span style="color: #00F;">NUMBER</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
    <span style="color: #00F;">SELECT</span> cxa_text
    <span style="color: #00F;">FROM</span>   clob_xfer_area
    <span style="color: #00F;">WHERE</span>  cxa_pk <span style="color: #00F;">=</span> p_cxa_pk
    <span style="color: #00F;">ORDER</span> <span style="color: #00F;">BY</span> cxa_number;
&nbsp;
<span style="color: #00F;">BEGIN</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
  <span style="color: #00F;">FOR</span> pk_rec <span style="color: #00F;">IN</span> pk_cur <span style="color: #00F;">LOOP</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
    <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span>CREATETEMPORARY<span style="color: #00F;">&#40;</span>v_new_clob<span style="color: #00F;">,</span> <span style="color: #00F;">TRUE</span><span style="color: #00F;">&#41;</span>;
    <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span><span style="color: #00F;">OPEN</span><span style="color: #00F;">&#40;</span>v_new_clob<span style="color: #00F;">,</span> <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span>LOB_READWRITE<span style="color: #00F;">&#41;</span>;
&nbsp;
    <span style="color: #00F;">FOR</span> piece_rec <span style="color: #00F;">IN</span> piece_cur<span style="color: #00F;">&#40;</span>pk_rec<span style="color: #00F;">.</span>cxa_pk<span style="color: #00F;">&#41;</span> <span style="color: #00F;">LOOP</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
      <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span>WRITEAPPEND<span style="color: #00F;">&#40;</span>v_new_clob<span style="color: #00F;">,</span> <span style="color: #000;">LENGTH</span><span style="color: #00F;">&#40;</span>piece_rec<span style="color: #00F;">.</span>cxa_text<span style="color: #00F;">&#41;</span><span style="color: #00F;">,</span>
        piece_rec<span style="color: #00F;">.</span>cxa_text<span style="color: #00F;">&#41;</span>;
&nbsp;
    <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span>;  <span style="color: #080; font-style: italic;">/* } of piece_cur */</span>
&nbsp;
    <span style="color: #00F;">DBMS_LOB</span><span style="color: #00F;">.</span><span style="color: #00F;">CLOSE</span><span style="color: #00F;">&#40;</span>v_new_clob<span style="color: #00F;">&#41;</span>;
&nbsp;
    <span style="color: #00F;">UPDATE</span> dest_table_with_clob
      <span style="color: #00F;">SET</span>  migrated_clob <span style="color: #00F;">=</span> v_new_clob
      <span style="color: #00F;">WHERE</span> dtwc_pk <span style="color: #00F;">=</span> pk_rec<span style="color: #00F;">.</span>cxa_pk;
&nbsp;
  <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span>; <span style="color: #080; font-style: italic;">/* } of pk_cur */</span>
&nbsp;
  <span style="color: #00F;">DELETE</span> <span style="color: #00F;">FROM</span> clob_xfer_area;
&nbsp;
<span style="color: #00F;">END</span>; <span style="color: #080; 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="plsql plsql" style="font-family:monospace;"><span style="color: #00F;">SET</span> lines <span style="color: #800;">1000</span>
<span style="color: #00F;">SET</span> pages 0
<span style="color: #00F;">SET</span> feedback <span style="color: #00F;">ON</span>
<span style="color: #00F;">SET</span> tab off
&nbsp;
<span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span>
<span style="color: #00F;">PROCEDURE</span> xfer_clobs
<span style="color: #00F;">IS</span>
&nbsp;
<span style="color: #00F;">BEGIN</span> <span style="color: #080; font-style: italic;">/* { */</span>
&nbsp;
  break_clobs_apart<span style="color: #00F;">@</span>source;
  put_clobs_together;
&nbsp;
<span style="color: #00F;">END</span>; <span style="color: #080; 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://djs-consulting.com/linux/blog/2007/transferring-clobs-across-linked-oracle-databases/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
