简体   繁体   中英

How to update CLOB column from a physical file?

I have a CLOB column in a table which holds very large amount of XML data. I need to update this column's value for one row of table. How can I do this?

I have tried googling, but this visibly simple stuff is not available in a simple language anywhere. Can anybody please suggest?

If I use the normal update query syntax and paste the huge xml content inside the single quotes (the single quote in xml content replaced with 2 single quotes), then the sql developer just disables the execute query button.

update tableName t 
set t.clobField = 'How to specify physical file data'
where t.anotherField='value'; 

You need to create a function that reads the data from the file into a variable of the CLOB data type and returns it as the result.

Try this working example:

create table tclob (id number, filename varchar2 (64), doc clob)
/
insert into tclob values (1, 'test.xml', empty_clob ());
commit;

create or replace function clobLoader (filename varchar2) return clob is
    bf bfile := bfilename ('TEMPFILES', filename);
    cl clob;
begin
    if dbms_lob.fileexists (bf) = 1 then
        dbms_lob.createtemporary (cl, true);
        dbms_lob.fileopen (bf, dbms_lob.file_readonly);
        dbms_lob.loadfromfile (cl, bf, dbms_lob.getlength (bf));
        dbms_lob.fileclose (bf);
    else cl := empty_clob ();
    end if;
    return cl;
end;
/

Usage:

update tclob t 
set t.doc = clobLoader (t.filename) 
where t.id = 1; 

1 row updated.

Search the internet for "load clob from file" and you will find a couple of examples. Such as this ( http://www.anujparashar.com/blog/loading-text-file-into-clob-field-in-oracle ):

DECLARE
    v_bfile    BFILE;
    v_clob    CLOB;
BEGIN
    v_bfile := BFILENAME (p_file_directory, p_file_name);

    IF DBMS_LOB.FILEEXISTS (v_bfile) = 1 THEN

        DBMS_LOB.OPEN (v_bfile);
        DBMS_LOB.CREATETEMPORARY (v_clob, TRUE, DBMS_LOB.SESSION);
        DBMS_LOB.LOADFROMFILE (v_clob, v_bfile, DBMS_LOB.GETLENGTH (v_bfile));
        DBMS_LOB.CLOSE (v_bfile);

        INSERT INTO tbl_clob (clob_col)
        VALUES (v_clob);
    END IF;

    COMMIT;
END;
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM