merge multiple varchar2 rows into one row of type CLOB [message #393613] |
Mon, 23 March 2009 10:52  |
JSF0
Messages: 3 Registered: March 2009
|
Junior Member |
|
|
There is something I am not understanding. How would I merge multiple varchar2 rows into one row of type CLOB though?
After inserting from table B into table A, I would like to see the following after selecting from table A:
keys text
----- -------
1 >TEXT<
How do I
merge multiple rows
into one row of type CLOB?
>/TEXT<
I wrote the following, but am unsure of how to utilize dbms_lob.writeAppend() with it:
create table A (keys number, text CLOB);
create table B (keys number, text varchar2);
insert into B values ('1', ">TEXT<");
insert into B values ('2', "How do I");
insert into B values ('3', " merge multiple rows");
insert into B values ('4', " into one row of type CLOB?");
insert into B values ('5', ">/TEXT<");
DECLARE
TYPE A_table_type IS TABLE OF A%ROWTYPE
INDEX BY BINARY_INTEGER;
t_A A_table_type;
CURSOR c1 IS
SELECT text FROM B WHERE keys BETWEEN 1 AND 5;
BEGIN
OPEN c1;
LOOP --Fetch a configured set of rows at once
FETCH c1
BULK COLLECT INTO t_A LIMIT 100;
EXIT WHEN t_A.COUNT = 0;
--For each set of rows fetched...
FOR x IN 1 .. t_A.COUNT LOOP
NULL; --dbms_lob.writeAppend(?????);
END LOOP;
END LOOP;
CLOSE c1;
END;
|
|
|
Re: Multiple records in one row [message #393628 is a reply to message #393613] |
Mon, 23 March 2009 13:04   |
 |
BlackSwan
Messages: 26766 Registered: January 2009 Location: SoCal
|
Senior Member |
|
|
truncate table B;
insert into B values ('1', ">TEXT<");
insert into B values ('10', ">/TEXT<");
insert into B values ('2', "How do I");
insert into B values ('9', " into one row of type CLOB?");
insert into B values ('3', " merge multiple rows");
insert into B values ('8', " merge multiple rows");
insert into B values ('4', " into one row of type CLOB?");
insert into B values ('7', "How do I");
insert into B values ('5', ">/TEXT<");
insert into B values ('6', ">TEXT<");
How do you decide which rows go in what order into table A?
Rows in a table have no inherent order & are like balls in a basket.
|
|
|
Re: Multiple records in one row [message #393629 is a reply to message #393628] |
Mon, 23 March 2009 13:36   |
JSF0
Messages: 3 Registered: March 2009
|
Junior Member |
|
|
Good catch. The tweaked code is below. I am still unsure if I am going in the right direction. I am trying to get it running in a proc, while figuring out how to use dbms_lob -if it is even appropriate.
create table A (keys number, text CLOB);
create table B (keys number, text varchar2(80));
insert into B values ('1', ">TEXT<");
insert into B values ('2', "How do I");
insert into B values ('3', " merge multiple rows");
insert into B values ('4', " into one row of type CLOB?");
insert into B values ('5', ">/TEXT<");
DECLARE
TYPE A_table_type IS TABLE OF A%ROWTYPE
INDEX BY BINARY_INTEGER;
t_A A_table_type;
CURSOR c1 IS
SELECT *
FROM B
WHERE keys BETWEEN 1 AND 5
ORDER BY keys;
BEGIN
OPEN c1;
LOOP
FETCH c1
BULK COLLECT INTO t_A LIMIT 100;
EXIT WHEN t_A.COUNT = 0;
FOR x IN 1 .. t_A.COUNT LOOP
dbms_lob.writeAppend(l_blob??, LENGTH(t_A.text(x)), t_A.text(x));
dbms_lob.writeAppend( l_blob??, 1, '0A' ); --newline
END LOOP;
END LOOP;
CLOSE c1;
END;
|
|
|
|
Re: Multiple records in one row [message #393738 is a reply to message #393629] |
Tue, 24 March 2009 04:15   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
I'd do something like:
DECLARE
CURSOR c1 IS
SELECT *
FROM B
WHERE keys BETWEEN 1 AND 5
ORDER BY keys;
v_clob clob;
BEGIN
FOR rec in c1 loop
v_clob := v_clob||to_clob(rec.text);
END LOOP;
insert into a values (1,v_clob);
END;
In 10g, Oracle automatically bulk fetches cursors with an array size of 100, so there's no real need to use explicit bulk fetches except in odd circumstances where you want to fetch <100 rows.
|
|
|
Re: Multiple records in one row [message #393822 is a reply to message #393630] |
Tue, 24 March 2009 09:06  |
JSF0
Messages: 3 Registered: March 2009
|
Junior Member |
|
|
Impressive. Thanks. Sorry for all the errors. I know it is hardly fun dealing with newbies. BTW -I replaced:
dbms_lob.writeAppend( l_blob, 1, '0A' ); --newline
with
dbms_lob.writeAppend( l_blob, 1, chr(10)||chr(13) ); --newline
and it works the way I want it to on UNIX and Windows. merci.
|
|
|