Reputation: 1
I'm getting a clob as parameter from a procedure and it contains a CSV file. I need to read this data and load it into another Oracle table.
Could someone please explain how to do this.
Upvotes: 0
Views: 14228
Reputation: 10648
AFAIK Oracle has no ready made goodies for this. One promising candidate is DBMS_UTILITY.COMMA_TO_TABLE, but it's heavily limited to a very special task making it no-option. So you have to roll your sleeves and make your own.
Your specification is a bit vague, but one option is a SPLIT
function:
create table so18t (
id number,
csv clob
);
insert all
into so18t values(1,'1,2,3'||chr(10)||
'40,5,6'||chr(10)||
'700,80,9'||chr(10))
into so18t values(2,'aaa,bbb,ccc'||chr(10)||
'ddd,eee,fff'||chr(10)||
'ggg,hhh,iii'||chr(10))
select 1 from dual;
declare
v_lines jh_util.stringlist_t;
v_values jh_util.stringlist_t;
begin
for rec in (select * from so18t order by id) loop
v_lines := jh_util.split(rec.csv, chr(10));
for i in v_lines.first .. v_lines.last loop
dbms_output.put_line('line ' || i || ':');
v_values := jh_util.split(v_lines(i));
/* Do what you want with the values - I just print them */
for j in v_values.first .. v_values.last loop
dbms_output.put_line('v_values(' || j || ') = ' || v_values(j));
end loop;
end loop;
end loop;
end;
/
show errors
Prints:
line 1:
v_values(1) = 1
v_values(2) = 2
v_values(3) = 3
line 2:
v_values(1) = 40
v_values(2) = 5
v_values(3) = 6
line 3:
v_values(1) = 700
v_values(2) = 80
v_values(3) = 9
line 1:
v_values(1) = aaa
v_values(2) = bbb
v_values(3) = ccc
line 2:
v_values(1) = ddd
v_values(2) = eee
v_values(3) = fff
line 3:
v_values(1) = ggg
v_values(2) = hhh
v_values(3) = iii
PL/SQL procedure successfully completed.
Of cource Oracle doesn't provide split but SO helps. In the example above I have used my own one.
Other interesting resources:
Upvotes: 3
Reputation: 38300
Do not export the data to a file.
You will need to convert the clob into something useful, parse it, then write to the other table. Here is the steps you need to do:
CSVReader reader = new CSVReader(the_reader_from_getCharacterStream);
ftwOracle's CLOB Object provides some useful methods.
CSVReader
is from Open CSV.
Upvotes: 2
Reputation: 2229
I don't know of an immediate way of parsing a clob as a CSV, but Oracle provides a number of tools for working with CSV files such as External Tables and SQL*Loader.
So an approach might be to:
Upvotes: 0