Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

oracle - Dumping CLOB fields into files?

Say you have the table:

Column_name | data_type
Title       | Varchar2
Text        | CLOB

with some rows:

SomeUnkownMovie | A long time ago in a galaxy far, far away....(long text ahead)
FredMercuryBio  | Awesomeness and stuff....(more long text)

Is there a way I could query that so it outputs files like

SomeUnkownMovie.txt
FredMercuryBio.txt

(and ofc, with their respective texts inside)

I reckon this should be a easy enough sqlplus script.. though I'm just not the one :(

thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This pl/sql code should work in oracle 11g. It dumps the text of the clobs into a directory with the title as filename.

BEGIN 
  FOR rec IN (
    select title, text 
    from mytable
  )
  LOOP 
    DBMS_XSLPROCESSOR.clob2file(rec.text, 'DUMP_SOURCES', rec.title ||'.txt'); 
  END LOOP;
END;

If DBMS_XSLPROCESSOR isn't available then you could replace DBMS_XSLPROCESSOR.clob2file with a procedure that uses UTL_FILE.

For example :

CREATE OR REPLACE PROCEDURE CLOB2FILE (
    clob_in IN CLOB,
    directory_name IN VARCHAR2,
    file_name IN VARCHAR2
)
IS
    file_handle UTL_FILE.FILE_TYPE;
    clob_part VARCHAR2(1024);
    clob_length NUMBER;
    offset NUMBER := 1;
BEGIN
    clob_length := LENGTH(clob_in);
    file_handle := UTL_FILE.FOPEN(directory_name, file_name, 'W');

    LOOP
        EXIT WHEN offset >= clob_length;
        clob_part := DBMS_LOB.SUBSTR (clob_in, 1024, offset);
        UTL_FILE.PUT(file_handle, clob_part);
        offset := offset + 1024;
    END LOOP;

    UTL_FILE.FFLUSH(file_handle);
    UTL_FILE.FCLOSE(file_handle);

EXCEPTION
    WHEN OTHERS THEN
        UTL_FILE.FCLOSE(file_handle);
        RAISE;

END;

Or perhaps replace DBMS_XSLPROCESSOR.clob2file with dbms_advisor.create_file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...