Home > Ask the Oracle Database / Applications Experts > Oracle PL/SQL Questions & Answers > How to concatenate rows into a single CLOB in PL/SQL
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

How to concatenate rows into a single CLOB in PL/SQL

Dan Clamage EXPERT RESPONSE FROM: Dan Clamage

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 16 April 2009
How would I merge multiple varchar2 rows into one row of type CLOB?

Example:

create table A (keys number, text CLOB);
create table B (keys number, text varchar2(100));

insert into B values (1, 'How do I '); insert into B values (2, ' merge multiple rows '); insert into B values (3, ' into one row of type CLOB? ');

After inserting from table B into table A, I would like to see the following after selecting from table A:

keys  text
----- -------
1     How do I
        merge multiple rows
          into one row of type CLOB?
I wrote the following, but am unsure of how to utilize dbms_lob.writeAppend() with it.
  DECLARE
  TYPE A_table_type IS TABLE OF A%ROWTYPE
  INDEX BY PLS_INTEGER;

  t_A A_table_type;

  CURSOR c1
  SELECT text FROM B WHERE keys BETWEEN 1 AND 3;

  BEGIN
  OPEN c1;
    LOOP --Fetch a configured set of rows at once
        FETCH c1
        BULK COLLECT INTO t_A LIMIT l_ROW_LIMIT;

        EXIT WHEN t_A.COUNT = 0;

          --For each set of rows fetched...
          FOR x IN 1 ..  t_A.COUNT LOOP
          dbms_lob.writeAppend(?????)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Oracle PL/SQL
Working with substitution variables and using EXECUTE IMMEDIATE in PL/SQL
How to open a ref cursor in a PL/SQL procedure
Converting Long Raw to Blob
Parsing in Oracle
ORA-01422 error when procedure returns more than one row
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers
Execute SQL statement from table in other schema
Script to revoke access from user
PL/SQL procedure to load CSV file into database table

Using Oracle PL/SQL
SELECT statement syntax and examples
Oracle PL/SQL tutorial
PL/SQL datatypes in Oracle
PL/SQL functions and triggers in Oracle
Stored procedures in PL/SQL
Working with substitution variables and using EXECUTE IMMEDIATE in PL/SQL
How to open a ref cursor in a PL/SQL procedure
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams
ORA-01422 error when procedure returns more than one row

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


I'm assuming you just want one row in table A, whose CLOB is the concatenation of a series of rows in B? Here is one approach.

I'd suggest you break the problem down into simpler units. One issue is how to concatenate the text values into a single CLOB. I wrote a stand-alone function to do just that:

create or replace
function assemble_clob(p_from in number, p_to in number)
return clob
is
  v_clob clob;  -- inner one
begin
  for rec in (
      select keys, text 
      from b 
      where keys between p_from and p_to)
  loop
    if (rec.keys = p_from) then  -- first item
      v_clob := rec.text;
    else  -- subsequent item
      v_clob := v_clob || CHR(10) || rec.text;  -- prepend newline
    end if;
  end loop;
  return (v_clob);
end assemble_clob;
/
Now I can use this in SQL:

insert into a (keys, text) values (1, assemble_clob(1, 3));

Notice how this simplifies the insert statement.

When I query table A, I get:

SQL> column text format a30
SQL> select * from a;

      KEYS TEXT
---------- ------------------------------
         1 How do I
             merge multiple rows
               into one row of type CLOB?




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts