Oracle数据库sqlldr工具的使用

sqlldr导入文本内容到数据库表时,需要指定一个ctl文件(控制文件),通过该文件来完成数据的导入。

1 首先创建一个表student

create table student(
       stu_id number(5) primary key,
       stu_name varchar2(32) not null,
       stu_age number(3),
       stu_sex number(1),
       stu_birth date
);
comment on table student is '学生表';
comment on column student.stu_sex is '学生性别,0:男,1:女';

创建一个txt或csv文件,文件内容如下

10001|tom|20|0|1993-01-01
10002|mary|18|1|1995-01-11
10003|小明|19|0|1993-03-13
10004|小芳|17|1|1994-11-23

2 创建student.ctl文件

load data
CHARACTERSET AL32UTF8
infile 'D:\test\student.txt'
append into table student
fields terminated by "|"
trailing nullcols
(stu_id,stu_name,stu_age,stu_se,stu_birth date "YYYY-MM-DD")

参数说明:

load data:控制文件标识
CHARACTERSET:设置编码格式,防止中文乱码
infile:指定数据文件路径
append:指定数据装载方式,共有四种取值:[(1) insert,为缺省方式,在数据装载开始时要求表为空;(2 )append,删除旧记录(用 delete from table 语句),替换成新装载的记录;(3) replace,删除旧记录,替换成新装载的记录;(4) truncate,删除旧记录(用 truncate table 语句),替换成新装载的记录];这里我指定的方式为append
fields terminated by:指定字段间的分隔符
trailing nullcols:表的字段没有对应的值时允许为空
(stu_id,stu_name,stu_age number,stu_se number,stu_birth date "YYYY-MM-DD"):定义列顺序

3 执行sqlldr导入数据

sqlldr userid=shiot/123456 control='D:/test/student.ctl' log='D:/test/student.log' bad='D:/test/student.bad'

参数说明:

userid:指定ORACLE 用户名/口令    
 control:指定控制文件路径         
 log:指定日志文件路径        
 bad:指定错误文件路径

猜你喜欢

转载自www.cnblogs.com/concurrencyy/p/9347330.html