Skip to Main Content
  • Questions
  • Inserting a file's contents in a clob type column

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sachi.

Asked: January 10, 2017 - 1:00 pm UTC

Last updated: January 11, 2017 - 1:24 am UTC

Version: 3.2.20.09

Viewed 10K+ times! This question is

You Asked

Hi,

I wish to insert a file into a clob type column along with values for the other fields. i dnt have priviledges to create directory ,henc i am not able to use the bfile load .The structure of my table is something like:

id:number populated by a trigger througha sequence
text: clob (here i want to insert the contents of a file)
Status: char(1byte)
date: date.

I have tried using SQl Loader to load but it is of no help. as i am getting a value exceed error even though i am only inserting character 'I'

Please let me know if i need to provide any more.

I did refer to the answer Tom had already given wherein he had written a function to pick up the file and then place in a clob column . but for that we need to create a directory but i dnt have sufficient priviledges for that.


with LiveSQL Test Case:

and Connor said...

If you are not using SQL Loader, you *will* need a directory object, eg, Here's a code snippet for you - see how this goes

SQL> declare
  2      l_blob  blob;
  3      l_clob  clob;
  4      l_bfile bfile;
  5  begin
  6      insert into demo values ( 1, empty_blob(), empty_clob() )
  7      returning theBlob, theClob into l_blob, l_clob;
  8
  9      l_bfile := bfilename( 'MY_FILES', 'asktom.rtf' );
 10      dbms_lob.fileopen( l_bfile );
 11      dbms_lob.loadfromfile( l_blob, l_bfile, dbms_lob.getlength( l_bfile ) );
 12      dbms_lob.fileclose( l_bfile );
 13
 14      l_bfile := bfilename( 'MY_FILES', 'asktom.txt' );
 15      dbms_lob.fileopen( l_bfile );
 16      dbms_lob.loadfromfile( l_clob, l_bfile, dbms_lob.getlength( l_bfile ) );
 17      dbms_lob.fileclose( l_bfile );
 18  end;
 19  /


Depending on your characterset, you might want to use DBMS_LOB.LOADCLOBFROMFILE instead of DBMS_LOB.LOADFROMFILE

If you are using SQL Loader, you can use a fully qualified name to pick up the file from the directory, eg

load data
infile *
replace
into table test_lob
fields terminated by ','
(
lob_id char(1),
lob_file FILLER char,
MY_LOB LOBFILE(lob_file) TERMINATED BY EOF
)
begindata
1,c:\data\doc\acct1.txt
2,c:\data\doc\acct2.txt 



Is this answer out of date? If it is, please let us know via a Comment

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here