How to upload OS files to LOB columns with Oracle SQL Loader

Links: http://www.dbatools.net/experience/sql_loader_lob_files.html

    By DBMS_LOB PL/SQL package I cannot load client OS files to LOB columns, you can either use lobs utility, or use Oracle's SQL Loader (sqlldr) to do this job.

    I have the following OS files in current directory.

IMG_A_1.jpg
IMG_A_2.jpg
IMG_B_1.jpg
IMG_B_2.jpg

    These files need to be uploaded into table IMAGES, which contains the following columns.

SQL> CREATE TABLE IMAGES
  2  (
  3     IMG_ID NUMBER(10) NOT NULL PRIMARY KEY,
  4     IMG_A  BLOB,
  5     IMG_B  BLOB
  6  )
  7  /

Table created.

    Let's prepare a text file (images.txt) for SQL Loader contains the following lines.

1,IMG_A_1.jpg,IMG_B_1.jpg
2,IMG_A_2.jpg,IMG_B_2.jpg

    Then we need to write a control file (images_sqlldr.ctl) for SQL Loader.

LOAD DATA
INFILE 'images.txt' "STR X'0d0a'"
INTO TABLE images
FIELDS TERMINATED BY ',' TRAILING NULLCOLS
(
  IMG_ID    CHAR ,
  LOBF_00001  FILLER  CHAR(32),
  IMG_A       LOBFILE(LOBF_00001) TERMINATED BY EOF ,
  LOBF_00002  FILLER  CHAR(32),
  IMG_B       LOBFILE(LOBF_00002) TERMINATED BY EOF
)

    Then we can use SQL Loader to load the image files into database.

Post a comment

« Previous | Main | Next »

Powered by
Movable Type 3.36