SQL*Loader 用法:
sqlldr keyword=value [,keyword=value,... ]
SQL*Loader通过控制文件实现对记录的处理,例子命令行下sqlldr scott/tiger control=case.ctl(并非数据库sql>下)
case.ctl内容如下:
load data
infile * ##使用*表示加载的数据在控制文件中,如果是独立文件要使用‘绝对路径名’
into table bonus ##指定要将数据加载到哪个表中,该表必须已经存在。into前还有参
##数使用说明如下:
##insert:向表中插入数据,表必须为空表否则执行失败。into默认
##为insert
##append:无论表中是否有数据都向表中追加数据 ##replace:将原来的表中数据delete干净,然后添加新数据
##truncate:truncate表中的数据,然后添加新数据
fields terminated by “,”##设置数据部分的字符串分割标志为
(ename,job,sal)##设置表的列名,列名必须与表中的列名相同。
begindata ##表示以下为待加载数据
smith,clerk,1900 ##格式要对应表列名
allen,salesman,2900
实验1:以独立文件向表中追加数据
vim case.ctl内容如下
load data
infile '/home/oracle/pks1.txt'
append into table bonus
fields terminated by ","
(ename,job,sal)
vim /home/oracle/psk1.txt内容如下:
ward,salsman,3123
king,president,5000
实验2:数据文件不符合规范,观察日志(与控制文件同名.log)以及错误文件(与日志文件同名.bad)
vim case1.ctl内容如下
load data
infile '/home/oracle/pks2.txt'
append into table bonus
fields terminated by ","
(ename,job,sal)
vim /home/oracle/psk2.txt内容如下:
kkksss123
wwwqqq321
[oracle@pks09 ~]$ cat case1.log
SQL*Loader: Release 10.2.0.1.0 - Production on Thu Jul 23 20:53:21 2015
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Control File: case1.ctl
Data File: /home/oracle/pks2.txt
Bad File: pks2.bad
Discard File: none specified
(Allow all discards)
Number to load: ALL
Number to skip: 0
Errors allowed: 50
Bind array: 64 rows, maximum of 256000 bytes
Continuation: none specified
Path used: Conventional
Table BONUS, loaded from every logical record.
Insert option in effect for this table: APPEND
Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
ENAME FIRST * , CHARACTER
JOB NEXT * , CHARACTER
SAL NEXT * , CHARACTER
Record 1: Rejected - Error on table BONUS, column JOB.
Column not found before end of logical record (use TRAILING NULLCOLS)
Record 2: Rejected - Error on table BONUS, column JOB.
Column not found before end of logical record (use TRAILING NULLCOLS)
Table BONUS:
0 Rows successfully loaded.
2 Rows not loaded due to data errors.
0 Rows not loaded because all WHEN clauses were failed.
0 Rows not loaded because all fields were null.
Space allocated for bind array: 49536 bytes(64 rows)
Read buffer bytes: 1048576
Total logical records skipped: 0
Total logical records read: 2
Total logical records rejected: 2
Total logical records discarded: 0
Run began on Thu Jul 23 20:53:21 2015
Run ended on Thu Jul 23 20:53:21 2015
Elapsed time was: 00:00:00.06
CPU time was: 00:00:00.02
观看日志文件可知pks2.txt数据文件的两行数据加载失败,错误文件为pks2.bad。
查看pks2.bad分析错误原因