1
votes

I would like to load a CSV file into an Oracle table. The file have a column that can contain a very long string that will exceed the maximum length for VARCHAR2. Because of that, the table I'm loading into have this column specified as BLOB.

create table MY_TABLE(
COL1 VARCHAR2(100),
COL2 VARCHAR2(100),
COL3 BLOB
);

The CSV file to load look like:

COL1,COL2,COL3
1,qwerty,<very long block of text>
2,asdfgh,<very long block of text>
3,zxcvbn,<very long block of text>
...

How can I load it using SQL Loader? All the sites I could find describe how to use BLOB in order to load separate files, ie:

LOAD DATA 
INFILE 'lob_test_data.txt'
   INTO TABLE lob_tab
   FIELDS TERMINATED BY ','
   (number_content    CHAR(10),
    varchar2_content  CHAR(100),
    date_content      DATE "DD-MON-YYYY" ":date_content",
    clob_filename     FILLER CHAR(100),
    clob_content      LOBFILE(clob_filename) TERMINATED BY EOF,
    blob_filename     FILLER CHAR(100),
    blob_content      LOBFILE(blob_filename) TERMINATED BY EOF)
    
    Source: https://oracle-base.com/articles/10g/load-lob-data-using-sql-loader

How can I load into BLOB from my single CSV file? I would expect that below code would work, but it throws a syntax error:

load data
inflie '/path/to/file.csv'
into table MY_TABLE
fields terminated by ','
(
COL1 CHAR(100),
COL2 CHAR(100),
COL3 BLOB
)
1
you want to load a very big text, then you should use CLOB , not BLOB. BLOB is for storing binary objects, as images, videos or sound files.Roberto Hernandez
do you have the text inside an external file, or the long text is inside the same csv file ??Roberto Hernandez

1 Answers

2
votes

In order to load a very long text, you should use CLOB instead of BLOB. The latter is for storing binary objects, such as video files, pictures or sound files.

The way to load depends whether the CLOB content is stored on your CSV file or you are using an external file locator.

CLOB INSIDE CSV FILE

Take this example. I create a csv example file with a very long text:

$ cat testclob.txt
1;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

My control file looks like

load data
infile '/home/ftpcpl/testclob.txt'
into table test_perf.t
append  
fields terminated by ';'
(
  c1 integer ,
  c2 char(10000)
)

Then I load the file

$ sqlldr test_perf/Oracle_1 control=/home/ftpcpl/testclob.ctl log=/home/ftpcpl/testclob.log

SQL*Loader: Release 12.2.0.1.0 - Production on Tue Aug 11 14:23:21 2020

Copyright (c) 1982, 2017, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 1

Table TEST_PERF.T:
  1 Row successfully loaded.

Check the log file:
  /home/ftpcpl/testclob.log
for more information about the load.

and the log file looks like

  Table TEST_PERF.T:
  1 Row successfully loaded.
  0 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null

.

CLOB AS EXTERNAL FILE LOCATOR

If the CLOB is inside another file, then you should load the file as:

load data
infile *
replace
into table t
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(
id integer external,
fname filler,
c LOBFILE(fname) TERMINATED BY EOF
)
begindata
1,"c:\temp\demo.out"
2,"c:\temp\load_emoji.sql"

SQL> create table t ( id int, c clob );

Table created.

$ sqlldr userid=scott/tigercontrol=c:\temp\c.ctl

SQL*Loader: Release 12.2.0.1.0 - Production on Fri Sep 8 15:49:55 2017

Copyright (c) 1982, 2017, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 1
Commit point reached - logical record count 2

Table T:
  2 Rows successfully loaded.

Check the log file:
  c.log
for more information about the load.

SQL> select id, length(c) from t;

        ID  LENGTH(C)
---------- ----------
         1      17640
         2       1418