Tuesday, October 23, 2007

[Example] SQL Loader some data into Table

Assume You had some data in sample.csv file(many many datas) and would like to load into user.sample table.
How can?

sample.csv file:

1,sample1
2,sample2
3,sample3
.
.
.


user.sample table:
SQL>desc user.sample
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
LOGIN VARCHAR2(50)

---- This case you should create control file for using sqlldr command.
#cat sample.ctrl
LOAD DATA
INFILE 'sample.csv' ---- load data from sample.csv file
BADFILE 'data.bad' ---- keep bad data in data.bad file
DISCARDFILE 'data.dsc' ---- some data that discard write to data.dsc file
APPEND ---- append data in table, if you would like delete and replace , that you can use "REPLACE"
INTO TABLE user.sample ----- table name
FIELDS TERMINATED BY ',' optionally enclosed by '"' TRAILING NULLCOLS ---- split data in file by use ","
(id,login) ---- fields in table


After create control file, We can use sqlldr command

$sqlldr control=sample.ctrl

However we should see sample.log file, that show error and data, that can load into table.

No comments: