`
zhb8015
  • 浏览: 390720 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
Group-logo
Spring Roo杂谈
浏览量:0
社区版块
存档分类
最新评论

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做导出、导入测试
这里需要一些设置满足数据导出的样式

vi exp_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.
复制代码

 

 

 

原文地址:http://www.cnblogs.com/cycsa/archive/2013/06/13/3134321.html

 

分享到:
评论

相关推荐

    oracle导出txt文件方法

    Oracle 文本导出的原理是将 SELECT 输出结果直接输出到文本文件中。使用 spool 文件名方式指定输出结果。 在使用 spool 命令时,我们需要设置一些参数以便于输出结果。这些参数包括: * set echo off:用于关闭...

    Oracle导入和导出工具使用

    这两种工具支持灵活的参数设置,可以根据具体需求选择导出或导入整个数据库、特定的表或特定用户的数据。 #### 二、使用exp.exe导出数据 ##### 2.1 完全导出数据库 ``` exp system/123456@ATSTestDB file=c:\...

    Oracle导出INSERT语句

    5. **保存与输出**:生成的INSERT语句会被保存到一个文本文件中,如"Oracle导出INSERT语句V1.0",用户可以根据需要在目标环境中执行这些语句来重新插入数据。 6. **安全考虑**:在实际使用过程中,需要注意数据隐私...

    Oracle导出成csv格式后显示乱码的解决方法

    在Excel的“文本导入向导”中,选择“分隔符号”选项卡,然后勾选“逗号”。这告诉Excel数据是以逗号为字段分隔符的。 6. **确认数据正确性**: 按照默认的其他设置继续,点击“完成”,Excel应该能够正确地解析...

    Oracle导出Clob,Blob工具版本2

    "Oracle导出Clob,Blob工具版本2" 是一个专为解决这个问题而设计的应用程序,它改进了对CLOB(Character Large Object)和BLOB(Binary Large Object)数据类型的导出效率,特别是修复了上一版本中导出时间过长的问题...

    通过PLSQL的进行oracle数据库导入导出

    【PL/SQL Developer 导入导出Oracle数据库详解】 在Oracle数据库管理中,高效的数据导入导出是确保数据安全和快速恢复的关键。PL/SQL Developer是一款强大的Oracle数据库开发工具,它提供了便捷的数据导出和导入...

    oracle导出数据库表结构到word文档中

    Oracle数据库表结构导出器是一套用来完成将Oracle数据库中的表结构导出成Word文档,并输出标准的打印报表格式的软件。亲测oracle绝对好使 ,另附有链接信息,请先看好说明再操作.不会的加我qq87611894

    oracle 常用语句 oracle备份 oracle冷备份 oracle启停 oracle导出 oracle导入

    --导入ORACLE参数 参数 说明 USERID 指定执行导入的用户名和密码 BUFFER 指定用来读取数据的缓冲区大小,以字节为单位 COMMIT 指定是否在每个数组(其大小由BUFFER参数设置)插入后进行提交 ..................(见...

    oracle快速导出.zip

    Oracle导出工具,通常指的是`expdp`(Data Pump Export),用于从数据库中导出数据和元数据。`expdp`提供了许多高级特性,如并行导出、过滤数据、压缩导出文件等,这些特性使得它成为处理大数据量的理想选择。在...

    Mysql to Oracle导入导出说明

    总的来说,从MySQL到Oracle的数据迁移涉及多个步骤,包括数据导出、转换、导入以及各种兼容性问题的处理。合理选择工具和策略,能有效保证迁移过程的顺利进行。在实际操作中,应结合具体情况进行详细规划和测试,以...

    PROC++批量导入导出ORACLE数据库表[借鉴].pdf

    ConnectDB函数用于连接ORACLE数据库,ImportTxtfile函数用于从文本文件中导入数据到数据库,而ExportTxtfile函数用于从数据库中导出数据到文本文件。 在编写这些函数时,需要包含必要的头文件,包括、、、、、、、...

    oracle导出excel和excel导入oracle的经验总结.docx

    以下是关于"Oracle导出Excel和Excel导入Oracle"的知识点总结: 1. **Oracle导出Excel** - **方法一**:通过Oracle SQL Developer进行导出。在SQL Developer中,用户可以在SQL窗口执行查询,然后利用工具栏的...

    Oracle大数据量数据导入导出示例

    ### Oracle大数据量数据导入导出的关键知识点 #### 一、Oracle大数据量数据导出操作 在Oracle数据库中,处理大数据量的数据导出通常涉及到优化查询性能、减少资源消耗以及确保数据完整性等方面。以下是对给定文件...

    oracle数据导出工具

    Oracle 数据导出工具,通常指的是 Oracle 的 Data Pump(也称为 EXPDP 和 IMPDP),这是一个高效的数据导入导出工具,用于在 Oracle 数据库之间迁移数据。本文将深入探讨 Oracle Data Pump 的工作原理、功能以及如何...

    oracle文本导入导出工具-ozoradts

    Import采用直接路径方式,等同于sqlldr的Direct方式导入(DIRECT=TRUE),nolog参数等同于sqldr的UNRECOVERABLE参数 ozoradts -exp userid=test/test@oradb columnover=";" query="select * from fct_serv_product_...

    Oracle数据库中业务数据文本导出.pdf

    不同于EXP和IMP工具,它们用于Oracle数据库之间的数据导入导出,UTL_FILE包导出的文件是文本格式,可以被任何支持文本导入的数据库系统所接受。使用这个包的一个关键优势在于,它减少了网络流量,因为数据处理和输出...

    sqlserver2005和oracle之间的导入导出

    - 如果数据是以CSV或其他文本格式导出的,则可以使用Oracle的`SQL*Loader`工具来导入数据。 #### 注意事项 - 在进行数据迁移之前,确保源数据库和目标数据库之间的兼容性问题得到解决,比如数据类型不匹配等问题...

    oracle导出excel和excel导入oracle的经验总结.pdf

    【Oracle导出Excel和Excel导入Oracle的经验总结】 在Oracle数据库管理中,经常需要将数据导出到Excel格式以便于分析和处理,同时也会有将Excel数据导入到Oracle表的需求。以下是一些常用的方法: 1. **Oracle导出...

Global site tag (gtag.js) - Google Analytics