Step 1

Create a table in Oracle with the same structure as in the CSV file by taking appropriate datatypes. For example, create a table like this:

LINUX> sqlplus user/password

SQL> CREATE TABLE jobs

(

st varchar2(2),

occ_code varchar2(7),

occ_title varchar2(30),

tot_emp number(10),

a_mean number(10,2),

a_pct10 number(10,2),

a_pct25 number(10,2),

a_median number(10,2),

a_pct75 number(10,2),

a_pct100 number(10,2)

);

Step 2

After creating the table, you have to write a control file describing the actions which SQL Loader should do. You can use any text editor to write the control file. Now let us write a controlfile.

LINUX> vi jobs.ctl

LOAD DATA

INFILE  ‘/home/oracle/jobs.csv’

BADFILE ‘/home/oracle/*user*/jobs.bad’

DISCARDFILE ‘/home/oracle/*user*/jobs.dsc’

INSERT INTO TABLE emp 

FIELDS TERMINATED BY “,” 

OPTIONALLY ENCLOSED BY ‘”’ TRAILING NULLCOLS

(empno,name,sal,jdate date ‘mm/dd/yyyy’)
  1. The LOAD DATA statement is required at the beginning of the control file.
  2. The INFILE option specifies where the input file is located
  3. Specifying BADFILE is optional. If you specify, then bad records found during loading will be stored in this file.
  4. Specifying DISCARDFILE is optional. If you specify, then records which do not meet a WHEN condition will be written to this file.
  5. You can use any of the following loading option: INSERT - Loads rows only if the target table is empty APPEND - Load rows if the target table is empty or not. REPLACE - First deletes all the rows in the existing table and then, load rows. TRUNCATE - First truncates the table and then load rows.
  6. This line indicates how the fields are separated in input file. Since in our case the fields are separated by “,” so we have specified “,” as the terminating char for fields. You can replace this by any char which is used to terminate fields. Some of the popularly use terminating characters are semicolon “;”, colon “:”, pipe “|” etc. TRAILING NULLCOLS means if the last column is null then treat this as null value, otherwise, SQL LOADER will treat the record as bad if the last column is null.
  7. In this line specify the columns of the target table. Note how do you specify format for Date columns

Step 3

After you have wrote the control file save it and then, call SQL Loader utility by typing the following command

LINUX> sqlldr userid=user/password control=jobs.ctl log=jobs.log

After you have executed the above command SQL Loader will shows you the output describing how many rows it has loaded.

The LOG option of sqlldr specifies where the log file of this sql loader session should be created. The log file contains all actions which SQL loader has performed i.e. how many rows were loaded, how many were rejected and how much time is taken to load the rows and etc. You have to view this file for any errors encountered while running SQL Loader.