How to read files into existing rows LOB columns in Oracle?
Question
I have to migrate data from one database to another. As preparation we have already extracted CLOB and BLOB data into separate files located on the Oracle server. The database contains several CLOB and BLOB columns in different tables that have been initialized with NULL values for all those rows.
How can I load the content of a file into the CLOB or BLOB column of an existing row. I know the exact touples of primary ID keys and filenames... but I've found no working way to read those files into the tables.
SQLLoader for example only seems to be able to add rows or to replace the complete table. But I only want to update single columns. We can use SQLLoader if needed or plain PL/SQL scripts - whatever works.
Thanks for all your help.
Solution
Somehting along the following lines should do the job. You'd want to parameterize it, and do a separate version for BLOB data. Not sure whether you have character set issues either.
declare
l_bfile bfile := bfilename('DATA_PUMP_DIR','cpy.log');
l_data clob;
l_soffset NUMBER := 1;
l_doffset NUMBER := 1;
l_lang NUMBER := 0;
l_warn NUMBER;
begin
DBMS_LOB.createtemporary (lob_loc => l_data,cache => TRUE,
dur => DBMS_LOB.call);
dbms_lob.fileopen( l_bfile, DBMS_LOB.file_readonly );
DBMS_LOB.loadclobfromfile(l_data, l_bfile, DBMS_LOB.LOBMAXSIZE,
l_soffset, l_doffset, 0, l_lang, l_warn );
dbms_lob.fileclose(l_bfile);
update f_lob set value = l_data where id = 1;
end;
/