--参考链接:http://blog.itpub.net/10951282/viewspace-757712/
--ctl中默认记录间的分割符是回车换行符,当记录中含回车换行符时,可运用str属性指定记录分隔符:infile test.dat "str '| "
--设置字符集:UTF8、AL32UTF8、ZHS16GBK
--select * from v$nls_parameters;
--select name, value$ from sys.props$ where name like 'NLS%';
OPTIONS (skip=1,rows=128)
LOAD DATA
CHARACTERSET ZHS16GBK --即本行代码为设置字符集
INFILE "D:oraclesqluldr2users_data.csv"
INFILE "D:oraclesqluldr2users_data1.csv"
INTO TABLE users_t TRUNCATE
FIELDS TERMINATED BY ","
--OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
virtual_column FILLER,
user_id,
user_name,
login_times,
last_login DATE "YYYY-MM-DD HH24:MI:SS"
)
--第二行还是导不进去,不知道为啥..连续分隔符?
--数据类型:DECIMAL EXTERNAL,INTEGER EXTERNAL,CHAR 不指定列类型时,默认为char
OPTIONS (skip=1,rows=128)
LOAD DATA
CHARACTERSET UTF8 --即本行代码为设置字符集
INFILE *
INTO TABLE users_t TRUNCATE
FIELDS TERMINATED BY x'09'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
virtual_column FILLER,
user_id INTEGER EXTERNAL, --不加
user_name "case when length(:user_name)=0 then null else :user_name end", --必须用双引号括起
login_times INTEGER EXTERNAL,
last_login DATE "YYYY-MM-DD HH24:MI:SS"
)
BEGINDATA
USER_ID USER_NAME LOGIN_TIMES LAST_LOGIN
1 1 Unmi 3 2020/11/16 17:00
2 2 5 2008/10/15
3 3 "隔叶 黄莺" 8 2009/1/2
4 4 Kypfos
5 5 不知秋 1 2008/12/23
--infile后根文件名默认扩展名为'.dat'
--操作类型:replace, insert(默认), truncate, append
LOAD DATA
INFILE *
INTO TABLE dept1 REPLACE
FIELDS TERMINATED BY x'09'
(
deptno,
dname,
loc
)
BEGINDATA
10 sales Virginia
20 accounting Virginia
--处理定长数据
LOAD DATA
INFILE 'account.dat' --数据同控制文件在一起时,用infile *
INTO TABLE count_trans append
WHERE year='1990'
(
account_nbr position(01:10) character,
day position(11:12) character,
month position(13:14) character,
transation_code position(15:16) character,
credit_amount position(17:30) character
)
INTO TABLE count REPLACE
WHERE year>'1990'
(
account_nbr position(01:10) character,
day position(11:12) character,
month position(13:14) character,
transation_code position(15:16) character,
credit_amount position(17:30) character
)
--处理变长数据
--可为列单独指定分隔符
--可以写多个 INFILE "another_data_file.csv" 指定多个外部数据文件
--还可以使用 BADFILE、DISCARDFILE 来指定坏数据和丢弃数据的文件,
--infile 'accounts' discardfile mtidsc.rec badfile mthad.rec
--control选项:skip=跳过行数,rows=多少条提交一次(默认64),errors=允许的错误记录数
--log=xx.log,bad=xx.bad,data=xxx(一般通过infile指定)
--log 记录日志文件,默认控制文件名去掉ctl,加log
--bad 坏数据文件,默认控制文件名去掉ctl,加badOPTIONS (skip=1,rows=128)
LOAD DATA
INFILE 'customer.dat'
INTO TABLE aa append
WHERE year='1990'
(
customer_id char terminated by '',
status char terminated by '',
dsc_class char enclosed by '',
source char terminated by whitespace
)
--对列进行操作
LOAD DATA
INFILE *
INTO TABLE dept1 REPLACE
FIELDS TERMINATED BY ','
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated date 'dd/mm/yyyy'
)
BEGINDATA
10,sales,Virginia,1/5/20000
20,accounting,Virginia,21/6/1999
--trailing nullcols 字段没有对应的值时允许为空
--字段拼接
LOAD DATA
INFILE *
INTO TABLE DEPT REPLACE
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated date 'dd/mm/yyyy',
enter_line ":deptno||:dname||:loc||:last_updated" --字段拼接
)
BEGINDATA
10,Sales,Virginia,1/5/2000
20,Accounting,Virginia,21/6/1999
30,Consulting,Virginia,5/1/2000
40,Finance,Virginia,15/3/2001
--条件赋值
LOAD DATA
INFILE *
INTO TABLE DEPT REPLACE
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "case when length(:last_updated)<=10 then to_date(:last_updated, 'dd/mm/yyyy')
else to_date(:last_updated, 'dd/mm/yyyy hh24:mi:ss') end"
)
BEGINDATA
10,Sales,Virginia,1/5/2000 12:03:03
20,Accounting,Virginia,21/6/1999
30,Consulting,Virginia,5/1/2000 01:23:00
40,Finance,Virginia,15/3/2001
--对于如下数据我们需自定义日期处理函数
BEGINDATA
10,Sales,Virginia,01-april-2001
20,Accounting,Virginia,13/04/2001
30,Consulting,Virginia,14/04/2001 12:02:02
40,Finance,Virginia,987268297
50,Finance,Virginia,02-apr-2001
60,Finance,Virginia,Not a date
--------
create or replace function my_to_date(p_string in varchar2) return date
as
type fmtArray is table or varchar2(25);
l_fmts fmtArray := fmtArray('dd-mon-yyyy','dd-month-yyyy',
'dd/mm/yyyy','dd/mm/yyyy hh24:mi:ss');
l_return date;
begin
for i in 1 .. l_fmts.count loop
begin
l_return := to_date(p_string, l_fmts(i))
exception when others then null;
end;
EXIT when l_return is not null;
end loop;
if(l_return is null) then
l_return := new_time(to_date('01011970','ddmmyyyy') + 1/24/60/60*p_string, 'GMT', 'EST')
end if;
return l_return;
end;
--可以指定filler列将其不导入数据库
--Optionally enclosed by '"' 表示数据中含“”括起来的字段,比如该字段中含分隔符等
--oracle处理部分必须用双引号括起来,oracle语法单引号表示字符串,防止冲突
LOAD DATA
INFILE *
INTO TABLE dept1 replace
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "case when length(:last_updated)<=10 then to_date(:last_updated, 'dd/mm/yyyy')
else to_date(:last_updated, 'dd/mm/yyyy hh24:mi:ss') end"
)
BEGINDATA
20,Something Not To BE Loaded,accounting,"Virginia,USA"
--sqlldr默认char类型输入字节流最大长度为255,超过需指定 char(N),对于较短数据也最好指定长度
OPTIONS (skip=1,rows=128)
LOAD DATA
INFILE *
INTO TABLE users append
WHEN LOGIN_TIMES<>'8'
FIELDS TERMINATED BY ","
TRAILING NULLCOLS
(
virtual_column FILLER,
--prod_id char(32) "trim(:prod_id)",
--acc_num char(20) "replace(:acc_num,chr(13),'')"
user_id "user_seq.nextval", --这一列直接取序列的下一值,而不用数据中提供的值
user_name "'Hi '||upper(:user_name)", -- 还能用SQL函数或运算对数据进行加工处理
login_times terminated by "," NULLIF(login_times='NULL'), --可为列单独指定分隔符
last_login DATE "YYYY-MM-DD HH24:MI:SS" NULLIF(last_login="NULL") -- 当字段为"NULL"时就是 NULL
)
BEGINDATA
,USER_ID,USER_NAME,LOGIN_TIMES,LAST_LOGIN
1,1,Unmi,3,2009-1-5 20:34
2,2,Fantasia,5,2008-10-15
3,3,隔叶黄莺,8,2009-1-2
4,4,Kypfos,NULL,NULL
5,5,不知秋,1,2008-12-23
连接方式:
sqlldr user/pwd control=webaccess.ctl
sqlldr test/test@orcl control=D:oraclesqluldr2users.ctl
sqlldr mh/mh@22.11.97.96:1521/ora10 control=fund_inf.ctl
--查看TERMINATED BY=x'09'和WHITESPACE的结果,WHITESPACE加载正常
LOAD DATA
INFILE *
INTO TABLE DEPT2
INSERT
FIELDS TERMINATED BY WHITESPACE
(DEPTNO, DNAME, LOC char(1000))
BEGINDATA
10 Sales Virginia
20 Accounting Virginia
30 Consulting Virginia
40 Finance Virginia
--也可以
LOAD DATA
INFILE *
INTO TABLE DEPT2
INSERT
FIELDS TERMINATED BY WHITESPACE
(DEPTNO, ad filler, DNAME, a2 filler, LOC char(1000))
BEGINDATA
10 Sales Virginia
20 Accounting Virginia
30 Consulting Virginia
40 Finance Virginia
--根据条件插入不同的表/分区,如一次装载所有分区,可通过装载表方式处理
into table account_trans
--into table sale partition(east_data)
when day between '01' and '31'
into table account_nbr
when account_type between 'aa' and 'zz'
--过滤开头#号的行
INTO TABLE <TABLE_NAME>
WHEN (1) <> '#'