您的位置:首页 > 数据库 > Oracle

sqlldr数据迁移导出Oracle中表的指定的数据、导入也可以

2013-05-31 14:36 351 查看
一、sqlldr导入txt

1.预备

a).txt文件

b).oracle建表

2.编写控制文件input_test.ctl

LOAD  DATA   

CHARACTERSET 'UTF8'    --字符集设定 
INFILE 'd:\input_test.txt'      --要导入的文本数据路径,可写多个
REPLACE into TABLE input_test  --追加方式导入
fields terminated by X'09'   --以制表符分隔
trailing nullcols                   --允许空列导入

(col1,col2)

注:

infile 'd:\input_test.txt'表示需要装载的数据文件的路径

append into table test 数据载入的表:
(1)append 表示表中有数据,加在后面
(2)INSERT 表示装入空表,有数据则停止。默认值
(3)REPLACE 原先表中如果有数据,会被删除
(4)TRUNCATE 如果要载入的数据与现在的数据相同,载入的数据替换现存的数据。
fields terminated by ',‘

表示数据用是','分隔的,用by X'09',即16进制的"09"代表TAB制表符,常用于excel转换的tab制表符文件的数据的导入。常用分隔符还有'|'

多语种可设置字符集编码为:CHARACTERSET 'UTF8'

3.DOS下执行

sqlldr system/psw@db, control=c:\input\input_test.ctl log=c:\input\input_test.log bad=c:\input\input_test.bad

有效的关键字: 

userid -- ORACLE username/password

control – 控制文件

log – 记录的日志文件

bad – 坏数据文件

data – 数据文件

discard – 丢弃的数据文件

discardmax – 允许丢弃数据的最大值 (全部默认)

skip -- Number of logical records to skip (默认0)

load -- Number of logical records to load (全部默认)

errors – 允许的错误记录数 (默认50)

rows -- Number of rows in conventional path bind array or between direct path data saves(每次提交的记录数,默认: 常规路径 64, 所有直接路径)

bindsize -- Size of conventional path bind array in bytes(默认256000)

每次提交记录的缓冲区的大小(字节为单位,默认256000)

silent --禁止输出信息 (header,feedback,errors,discards,partitions)

direct – 使用直通路径方式导入 (默认FALSE)

parfile -- parameter file: name of file that contains parameter specifications

parallel -- 并行导入 (默认FALSE)

file -- File to allocate extents from

skip_unusable_indexes -- disallow/allow unusable indexes or index partitions(默认FALSE)

skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable(默认FALSE)

readsize -- Size of Read buffer (默认1048576)

与bindsize成对使用,其中较小者会自动调整到较大者。sqlldr先计算单条记录长度,乘以rows,如小于bindsize,不会试图扩张rows以填充bindsize;如超出,则以bindsize为准。

external_table -- use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE(默认NOT_USED)

columnarrayrows -- Number of rows for direct path column array(默认5000)

streamsize -- Size of direct path stream buffer in bytes(默认256000)

multithreading -- use multithreading in direct path

resumable -- enable or disable resumable for current session(默认FALSE)

resumable_name -- text string to help identify resumable statement

resumable_timeout -- wait time (in seconds) for RESUMABLE(默认7200)

date_cache -- size (in entries) of date conversion cache(默认1000)

 

4.写成.bat批处理

上述3步已完成了txt导入,在windows下还可将sqlldr命令写成批处理文件,双击执行.

@echo off

echo input_test    

pause     --暂停,建议加入,以免错误双击执行
@rem
sqlldr system/psw@db, control=c:\input\input_test.ctl log=c:\input\input_test.log bad=c:\input\input_test.bad
@rem
@rem sqlldr system/psw@db, control=c:\input\input_test.ctl rows=100000
pause

 

二、sqlldr导出txt

利用spool 导出txt

1.写output.sql

sqlplus system/psw@db as sysdba  --连接oracle
CHARACTERSET al32UTF8            --设置编码集
set trimspool on                               --打开池
spool c:\output\output.txt                  --池输出路径
set pagesize 0                                 --页设置
set heading off                                 --关闭表头
set linesize 32767                            --最大行显
select '#'||col1||'#,#'||col2||'#,#'||col3||'#‘  --设置需要的列格式。此例,列间以以逗号分隔,列内容用#引起。

from test_data;
exit;                                                 --退出sqlplus
spool off                                          --关闭池
pause

注:上述命令在dos下直接敲命令执行亦可。

2.写成.bat批处理

再写bat调用上面的outputsql文件,如下:

cd/
set NLS_LANG=.AL32UTF8    --设置字符集utf8
chcp 65001                             --转换编码页utf8
@echo off
echo  data output
pause
@rem
 sqlplus system/psw@db as sysdba @c:\dmp_sql\output.sql 
@rem 
pause
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: