Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Heru.

Asked: August 16, 2017 - 3:09 pm UTC

Last updated: August 16, 2017 - 3:41 pm UTC

Version: Oracle11g

Viewed 1000+ times

You Asked

Dear Sir,
I have a problem using sqlldr where I need to add a value that does not exist in the user text file but this value is needed in the table once the data from that text file is inserted into the table.
For example:
User text file has values from the following columns separated by tabs:
id > FName > LName > MName

1234 > Caroline> Smith > K.
1232 > Sharon > Allen > J.
3424 > Harry > Ho >
5436 > Kim > Maw > R.

But the table has an extra column (Institution) where it's value is not in the text file but it is needed for us to differentiate the users once the data is inserted into the table.

The table has this columns:
id > FName > LName > MName > Institution

Once the sqlldr is done running, my table should have content like this:

1234 > Caroline> Smith > K. > UM
1232 > Sharon > Allen > J. > UM
3424 > Harry > Ho > > UM
5436 > Kim > Maw > R. > UM

If columns in my control file (.ctl) match the total column of my table, my sqlldr ran perfectly.
So:
Text file : id > FName > LName > MName
Table : id > FName > LName > MName >> in this scenario I have no problem running sqlldr from my web application

BUT:
id > FName > LName > MName
id > FName > LName > MName > Institution >> in this situation I get error from sqlldr saying :

Record 1: Rejected - Error on table UM_VALID2, column INSTITUTION.

Is there a way to add Institution name that is not in the text file but this name get inserted into the table when running sqlldr?








and Chris said...

So you want to supply a default value for columns missing at the end of records in your source file?

If so, using the "trailing nullcols" option and provide a default in the column spec.

For example, if your table is:

create table t (
  x int, y int, z int
);


The following control file only has values for the first two columns. But it gives the default of 0 for the final column:

load data
infile *
into table t
fields terminated by "\t" optionally enclosed by '"'
TRAILING NULLCOLS
( X, Y, Z "0"
)
begindata
1 1
2 2


So when you use it:

sqlldr userid=chris/*****@db control=ldr.ctl


It loads zero into Z:

select * from t;

X  Y  Z  
1  1  0  
2  2  0 


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

More to Explore

Utilities

All of the database utilities are explained in the Utilities guide.