威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
数据库>> Mysql Sqlserver Oracle SQlite Access Sybase SQL其他
当前位置:首页 > 数据库 > Oracle
上一节 下一节
 Oracle11g数据装载的几种方式

数据的装载:

sql*loader:

sql*loader是一个oracle工具,,能够将数据从外部数据文件装载到数据库中。

运行sql*loader的命令是sqlldr。

sqlldr的两种使用方式:

1. 只使用一个控制文件,在这个控制文件中包含数据

2. 使用一个控制文件(作为模板) 和一个数据文件

一般采用第二种方式,数据文件可以是 csv 文件、txt文件或者以其他分割符分隔的。

说明:操作类型 可用以下中的一值:

1) insert --为缺省方式,在数据装载开始时要求表为空

2) append --在表中追加新记录

3) replace --删除旧记录(用 delete from table 语句),替换成新装载的记录

4) truncate --删除旧记录(用 truncate table 语句),替换成新装载的记录

通过spool来制作数据文件:--可以查询帮助文档的示例代码

sql> spool /u01/app/oracle/test_data_loader/student.txt--开启spool导出数据文件
sql> select id ||',' || name ||',' || age ||',' || inner_date from student;--导出数据

id||','||name||','||age||','||inner_date
--------------------------------------------------------------------------------
1,zhangsan,21,23-jan-15
2,lisi,22,23-jan-15
3,wangwu,23,23-jan-15

sql> spool off;--关闭
sql> exit
disconnected from oracle database 11g enterprise edition release 11.2.0.1.0 - 64bit production
with the partitioning, olap, data mining and real application testing options
[oracle@localhost test_data_loader]$ cat student.txt--可以查看到导出的数据记录
sql> select id ||',' || name ||',' || age ||',' || inner_date from student;

id||','||name||','||age||','||inner_date
--------------------------------------------------------------------------------
1,zhangsan,21,23-jan-15
2,lisi,22,23-jan-15
3,wangwu,23,23-jan-15

sql> spool off;

写配置文件:
[oracle@localhost test_data_loader]$ vi student.ctl
[oracle@localhost test_data_loader]$ cat student.ctl
options(skip=4)--表示前面的四行
load data--导入数据
infile 'student.txt'--通过该文件导入数据
into table student--导入的表
insert--执行的是插入操作
fields terminated by ','--记录中的分割符
(
id char,--注意虽然表中是number类型,但是要写char类型
name char,
age char,
inner_date date nullif (inner_date = "null"))
[oracle@localhost test_data_loader]$

既然是insert操作所以:
sql> truncate table student;--清空表,由于执行的是插入操作

table truncated.

sql> select * from student;

no rows selected

执行sqlldr操作:
[oracle@localhost test_data_loader]$ sqlldr hr/hr control= student.ctl log = student.log

sql*loader: release 11.2.0.1.0 - production on fri jan 23 23:11:08 2015

copyright (c) 1982, 2009, oracle and/or its affiliates. all rights reserved.

commit point reached - logical record count 4
[oracle@localhost test_data_loader]$ cat student.log

sql*loader: release 11.2.0.1.0 - production on fri jan 23 23:11:08 2015

copyright (c) 1982, 2009, oracle and/or its affiliates. all rights reserved.

control file: student.ctl
data file: student.txt
bad file: student.bad
discard file: none specified

(allow all discards)

number to load: all
number to skip: 4
errors allowed: 50
bind array: 64 rows, maximum of 256000 bytes
continuation: none specified
path used: conventional

table student, loaded from every logical record.
insert option in effect for this table: insert

column name position len term encl datatype
------------------------------ ---------- ----- ---- ---- ---------------------
id first * , character
name next * , character
age next * , character
inner_date next * , date dd-mon-rr
null if inner_date = 0x6e756c6c(character 'null')

record 4: rejected - error on table student, column id.
column not found before end of logical record (use trailing nullcols)

table student:
3 rows successfully loaded.
1 row 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: 66048 bytes(64 rows)
read buffer bytes: 1048576


申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
Mysql
Sqlserver
Oracle
SQlite
Access
Sybase
SQL其他

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821