oracle导出数据到文本、从文本导入数据的详细步骤

oracle导出数据到文本、从文本导入数据的详细步骤

经常有需求向表中导入大量的数据,使用insert不靠谱,太慢了,oracle提供了sqlldr的工具

也有时需要讲数据导入到文本,oracle的spool可以轻松实现oracle导出数据到txt、txt导入数据到oracle

 

一、导出数据到txt

这里用all_objects表做测试

sql> desc all_objects;
name                                      null?    type
----------------------------------------- -------- ----------------------------
owner                                     not null varchar2(30)
object_name                               not null varchar2(30)
subobject_name                                     varchar2(30)
object_id                                 not null number
data_object_id                                     number
object_type                                        varchar2(19)
created                                   not null date
last_ddl_time                             not null date
timestamp                                          varchar2(19)
status                                             varchar2(7)
temporary                                          varchar2(1)
generated                                          varchar2(1)
secondary                                          varchar2(1)

拿object_id,object_name做导出、导入测试
这里需要一些设置满足数据导出的样式

viexp_table.sql

set line 1000         --设置行的长度
set pagesize 0        --输出不换页
set feedback off      --默认的当一条sql发出的时候,oracle会给一个反馈,比如说创建表的时候,如果成功命令行会返回类似:table created的反馈,off后不显示反馈
set heading off       --不显示表头信息
set trimspool on      --如果trimspool设置为on,将移除spool文件中的尾部空
set trims on          --去掉空字符
set echo off;       --显示start启动的脚本中的每个sql命令,缺省为on
set colsep '|'         --设置分隔符
set termout off        --不在屏幕上显示结果
spool db1.txt          --记录数据到db1.txt
select object_id,object_name from all_objects;  --导出数据语句
spool off              --收集完毕
exit

一切就绪后导出数据

[oracle@centos5 ~]$ sqlplus test/test @exp_table.sql 

sql*plus: release 10.2.0.4.0 - production on thu jun 13 16:35:14 2013

copyright (c) 1982, 2007, oracle.  all rights reserved.


connected to:
oracle database 10g enterprise edition release 10.2.0.4.0 - 64bit production
with the partitioning, olap, data mining and real application testing options

disconnected from oracle database 10g enterprise edition release 10.2.0.4.0 - 64bit production
with the partitioning, olap, data mining and real application testing options
[oracle@centos5 ~]$ sed -i 's/ //g' db1.txt  --可选,去除每行开头部分的空格
[oracle@centos5 ~]$ more db1.txt 20|icol$
44|i_user1
28|con$
15|undo$
29|c_cobj#
3|i_obj#
25|proxy_role_data$

导出后检查数据的记录数是否正确

[oracle@centos5 ~]$ cat db1.txt |wc -l
49988
[oracle@centos5 ~]$ sqlplus test/test

sql*plus: release 10.2.0.4.0 - production on thu jun 13 16:36:21 2013

copyright (c) 1982, 2007, oracle.  all rights reserved.


connected to:
oracle database 10g enterprise edition release 10.2.0.4.0 - 64bit production
with the partitioning, olap, data mining and real application testing options

sql> select count(*) from all_objects;

count(*)
----------
   49988  --数据正确

 

二、从txt导入数据到oracle

sqlldr是通过一个control文件设定后,从文本导入数据

建立一张测试表

sql> create table tb_sqlldr (id number,name varchar2(50));

table created.

建立一个control文件

vi tb_sqlldr.ctl

load data                 
infile 'db1.txt'            --数据来源文本
append into table tb_sqlldr    --数据导入到表tb_sqldr中,导入方式为追加,如果想覆盖
fields terminated by "|"    --4、字段终止于x'09',是一个制表符(tab)
(id,name)                    --定义对应的字段名称,注意顺序

导入数据分成四种模式,可以根据需求选择:

append // 原先的表有数据 就加在后面
insert // 装载空表 如果原先的表有数据 sqlloader会停止 默认值
replace // 原先的表有数据 原先的数据会全部删除
truncate // 指定的内容和replace的相同 会用truncate语句删除现存数据

执行导入操作

sqlldr userid=test/test control=tb_sqlldr.ctl

差不多5w的数据短短2s解决

执行导入后验证数据

sql> select count(*) from tb_sqlldr;

count(*)
----------
   49988

导入成功

再执行一次导入操作,由于设置为追加

sql> select count(*) from tb_sqlldr;

count(*)
----------
   99976

记录翻倍

sqlldr还有很多参数供选择,比如log、bad这些,查看帮助即可

[oracle@centos5 ~]$ sqlldr

sql*loader: release 10.2.0.4.0 - production on thu jun 13 17:07:26 2013

copyright (c) 1982, 2007, oracle.  all rights reserved.


usage: sqlldr keyword=value [,keyword=value,...]

valid keywords:

  userid -- oracle username/password           
 control -- control file name                  
     log -- log file name                      
     bad -- bad file name                      
    data -- data file name                     
 discard -- discard file name                  
discardmax -- number of discards to allow          (default all)
    skip -- number of logical records to skip    (default 0)
    load -- number of logical records to load    (default all)
  errors -- number of errors to allow            (default 50)
    rows -- number of rows in conventional path bind array or between direct path data saves
             (default: conventional path 64, direct path all)
bindsize -- size of conventional path bind array in bytes  (default 256000)
  silent -- suppress messages during run (header,feedback,errors,discards,partitions)
  direct -- use direct path                      (default false)
 parfile -- parameter file: name of file that contains parameter specifications
parallel -- do parallel load                     (default false)
    file -- file to allocate extents from      
skip_unusable_indexes -- disallow/allow unusable indexes or index partitions  (default false)
skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable  (default false)
commit_discontinued -- commit loaded rows when load is discontinued  (default false)
readsize -- size of read buffer                  (default 1048576)
external_table -- use external table for load; not_used, generate_only, execute  (default not_used)
columnarrayrows -- number of rows for direct path column array  (default 5000)
streamsize -- size of direct path stream buffer in bytes  (default 256000)
multithreading -- use multithreading in direct path  
resumable -- enable or disable resumable for current session  (default false)
resumable_name -- text string to help identify resumable statement
resumable_timeout -- wait time (in seconds) for resumable  (default 7200)
date_cache -- size (in entries) of date conversion cache  (default 1000)

please note: command-line parameters may be specified either by
position or by keywords.  an example of the former case is 'sqlldr
scott/tiger foo'; an example of the latter is 'sqlldr control=foo
userid=scott/tiger'.  one may specify parameters by position before
but not after parameters specified by keywords.  for example,
'sqlldr scott/tiger control=foo logfile=log' is allowed, but
'sqlldr scott/tiger control=foo log' is not, even though the
position of the parameter 'log' is correct.

关于oracle导出数据到文本、从文本导入数据的详细步骤的文章就介绍至此,更多相关oracle导出数据到文本内容请搜索硕编程以前的文章,希望以后支持硕编程

下一节:oracle导出文本文件的三种方法(spool,utl_file,sqluldr2)

oracle数据库

相关文章
学习SQLite