8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » 8i » Here

Import CLOB

Related articles.

The following article presents a simple methods for importing a file into a CLOB datatype. First a directory object is created to point to the relevant filesystem directory.

create or replace directory documents AS 'C:\';

Next we create a table to hold the CLOB.

create table tab1 (
  id        number,
  clob_data clob
);

We import the file into a CLOB datatype and insert it into the table.

declare
  l_bfile  bfile;
  l_clob   clob;

  l_dest_offset   integer := 1;
  l_src_offset    integer := 1;
  l_bfile_csid    number  := 0;
  l_lang_context  integer := 0;
  l_warning       integer := 0;
begin
  insert into tab1 (id, clob_data)
  values (1, empty_clob())
  return clob_data into l_clob;

  l_bfile := bfilename('DOCUMENTS_DIR', 'Sample.txt');
  dbms_lob.fileopen(l_bfile, dbms_lob.file_readonly);
  -- loadfromfile deprecated.
  -- dbms_lob.loadfromfile(l_clob, l_bfile, dbms_lob.lobmaxsize);
  dbms_lob.loadclobfromfile (
    dest_lob      => l_clob,
    src_bfile     => l_bfile,
    amount        => dbms_lob.lobmaxsize,
    dest_offset   => l_dest_offset,
    src_offset    => l_src_offset,
    bfile_csid    => l_bfile_csid ,
    lang_context  => l_lang_context,
    warning       => l_warning);
  dbms_lob.fileclose(l_bfile);

  commit;
end;
/

To update an existing CLOB do the following.

DECLARE
  l_bfile  bfile;
  l_clob   clob;

  l_dest_offset   integer := 1;
  l_src_offset    integer := 1;
  l_bfile_csid    number  := 0;
  l_lang_context  integer := 0;
  l_warning       integer := 0;
begin
  select clob_data
  into   l_clob
  from   tab1
  where  id = 1
  for update;

  l_bfile := bfilename('DOCUMENTS_DIR', 'Sample.txt');
  dbms_lob.fileopen(l_bfile, dbms_lob.file_readonly);
  dbms_lob.trim(l_clob, 0);
  -- loadfromfile deprecated.
  -- dbms_lob.loadfromfile(l_clob, l_bfile, dbms_lob.getlength(l_bfile));
  dbms_lob.loadclobfromfile (
    dest_lob      => l_clob,
    src_bfile     => l_bfile,
    amount        => dbms_lob.lobmaxsize,
    dest_offset   => l_dest_offset,
    src_offset    => l_src_offset,
    bfile_csid    => l_bfile_csid ,
    lang_context  => l_lang_context,
    warning       => l_warning);
  dbms_lob.fileclose(l_bfile);
end;
/

In reality you would probably put this code into a stored procedure, or packaged procedure. This example is available here.

CREATE OR REPLACE PROCEDURE file_to_clob (p_clob      IN OUT NOCOPY CLOB,
                                          p_dir       in  varchar2,
                                          p_filename  in  varchar2)
as
  l_bfile  bfile;

  l_dest_offset   integer := 1;
  l_src_offset    integer := 1;
  l_bfile_csid    number  := 0;
  l_lang_context  integer := 0;
  l_warning       integer := 0;
begin
  l_bfile := bfilename(p_dir, p_filename);
  dbms_lob.fileopen(l_bfile, dbms_lob.file_readonly);
  dbms_lob.trim(p_clob, 0);
  dbms_lob.loadclobfromfile (
    dest_lob      => p_clob,
    src_bfile     => l_bfile,
    amount        => dbms_lob.lobmaxsize,
    dest_offset   => l_dest_offset,
    src_offset    => l_src_offset,
    bfile_csid    => l_bfile_csid ,
    lang_context  => l_lang_context,
    warning       => l_warning);
  dbms_lob.fileclose(l_bfile);
end file_to_clob;
/

You might call this like the following.

declare
  l_clob   clob;
begin
  select clob_data
  into   l_clob
  from   tab1
  where  id = 1
  for update;

  file_to_clob (p_clob     => l_clob,
                p_dir      => 'DOCUMENTS_DIR',
                p_filename => 'Sample.txt');
end;
/

For more information see:

Hope this helps. Regards Tim...

Back to the Top.