代码改变世界

Oracle sqlldr导入导出txt数据文件详解

2015-07-16 14:10  假面Wilson  阅读(7216)  评论(0编辑  收藏  举报

一、sqlldr导入txt

1.预备

a).txt文件

这里要保存成无签名的UTF-8

b).oracle建表

2.编写控制文件input_test.ctl

LOAD DATA

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

(col1,col2)

当col1为日期类型时,在控制文件中的字段里把col1改成
col1 "to_date(:col1,'''yyyy-mm-dd hh24:mi:ss''')"即可解决。

注:

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

@db  -- 链接适配器名称(Net Manager配置的链接名称)

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

 

 

ctl文件的例子:

OPTIONS(skip_index_maintenance=TRUE,direct=true,BINDSIZE=20971520,READSIZE=20971520,ERRORS=-1,ROWS=500000)
--unrecoverable
LOAD DATA
--CHARACTERSET AL32UTF8
INFILE 'c:\input\Barcode.txt' ---------数据文件,即txt文件
Append INTO TABLE Demoaa.TMS_BRANCHCODE -----表名
FIELDS TERMINATED BY X'09' ------数据用制表符分割
trailing nullcols
(ID ------表中字段
,Branch_plant
,SO_Number
,Trip_Number
,Shippment_Date "to_date(:Shippment_Date,'''yyyy-mm-dd hh24:mi:ss''')"
,Sold_to
,Sold_to_Name
,Ship_to
,Ship_to_Name
,BarCode_Info
,Barcode_Seg_1
,Barcode_Seg_2
,Barcode_Seg_3
,Barcode_Seg_4
,Barcode_Seg_5
,Last_updated_time "to_date(:Last_updated_time,'''yyyy-mm-dd hh24:mi:ss''')"
)