2021-03-24

1、普通装载
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘"’
(DEPTNO,
DNAME,
LOC
)
BEGINDATA
10,Sales,""“USA”""
20,Accounting,“Virginia,USA”
30,Consulting,Virginia
40,Finance,Virginia
50,“Finance”,"",Virginia --loc 列将为空
60,“Finance”,Virginia --loc 列将为空
注:BEGINDATA后的数值前面不能有空格

2、TERMINATED BY WHITESPACE 和 FIELDS TERMINATED BY x’09’ 的情况
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY WHITESPACE
–FIELDS TERMINATED BY x’20’
(DEPTNO,
DNAME,
LOC
)
BEGINDATA
10 Sales Virginia

注:x’20’表示字符ASCII码的16进制数值

3、指定不装载那一列
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘"’
(DEPTNO,
FILLER_1 FILLER, --下面的 “Something Not To Be Loaded” 将不会被装载
DNAME,
LOC
)
BEGINDATA
20,Something Not To Be Loaded,Accounting,“Virginia,USA”

4、position的列子
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
(DEPTNO position(1:2),
DNAME position(:16), --这个字段的开始位置在前一字段的结束位置
LOC position(
:29),
ENTIRE_LINE position(1:29)
)
BEGINDATA
10Accounting Virginia,USA
结果:10 | Accounting Vir | ginia,USA | 10Accounting Virginia,USA

5、使用函数日期的一种表达TRAILING NULLCOLS的使用
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ‘,’
TRAILING NULLCOLS
–这句的意思是将没有对应值的列都置为null
–如果第一行改为 10,Sales,Virginia,1/5/2000, 就不用TRAILING NULLCOLS了
(DEPTNO,
DNAME “upper(:dname)”, --使用函数
LOC “upper(:loc)”,
LAST_UPDATED date ‘dd/mm/yyyy’, --日期的一种表达方式。还有’dd-mon-yyyy’等
ENTIRE_LINE “:deptno||:dname||:loc||:last_updated”
)
BEGINDATA
10,Sales,Virginia,1/5/2000
20,Accounting,Virginia,21/6/1999
30,Consulting,Virginia,5/1/2000
40,Finance,Virginia,15/3/2001
注:可以通过:dname类型调用函数,特别注意date函数的使用。

6、 合并多行记录为一行记录

LOAD DATA
INFILE *
concatenate 3 --通过关键字concatenate 把几行的记录看成一行记录
INTO TABLE DEPT
replace
FIELDS TERMINATED BY ‘,’
(DEPTNO,
DNAME “upper(:dname)”,
LOC “upper(:loc)”,
LAST_UPDATED date ‘dd/mm/yyyy’
)
BEGINDATA
10,Sales,
Virginia,
1/5/2000
注:例如有些文本文件以N行为一循环记录数据,则可以这样导入。

7、使用continueif来合并记录行

上例可直接使用continueif last= ',‘来告诉Oracle如果前一个数据以’,'结尾,则这个附加到上一行

LOAD DATA
INFILE *
continueif last= ‘,’
INTO TABLE DEPT
replace
FIELDS TERMINATED BY ‘,’
(DEPTNO,
DNAME “upper(:dname)”,
LOC “upper(:loc)”,
LAST_UPDATED date ‘dd/mm/yyyy’
)
BEGINDATA
10,Sales,
Virginia,
1/5/2000

注:ContinueIf还可以使用this或next选项,具体操作见文档

8、载入每行的行号

LOAD DATA
INFILE *
INTO TABLE DEPT
replace
(DEPTNORECNUM //载入每行的行号
ENTIRE_LINEPosition(1:1024)
)
BEGINDATA
fsdfasj --自动分配行号到DEPTNO字段,此行为1
fasdjfasdfl --自动递增,此行为2

9、载入有换行符的数据

①使用一个非换行符的字符
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ‘,’
TRAILING NULLCOLS
(DEPTNO,
DNAME “upper(:dname)”,
LOC “upper(:loc)”,
LAST_UPDATED “my_to_date( :last_updated )”,
COMMENTS “replace(:comments,’%%’,chr(10))” --用replace函数转换成换行符
)
BEGINDATA
10,Sales,Virginia,01-april-2001,This is the Sales%%Office in Virginia
20,Accounting,Virginia,13/04/2001,This is the Accounting%%Office in Virginia
30,Consulting,Virginia,14/04/2001 12:02:02,This is the Consulting%%Office in Virginia
40,Finance,Virginia,987268297,This is the Finance%%Office in Virginia
注:换行的特殊字符如果使用’\n’,则会在Windows编译过程中直接换成换行符,导致无法转换

②使用fix属性

Load DATA
INFILE demo1.dat “fix 68”
INTO TABLE t1_a
REPLACE
FIELDS TERMINATED BY ‘,’
TRAILING NULLCOLS
(DEPTNO,
DNAME “upper(:dname)”,
LOC “upper(:loc)”,
LAST_UPDATED Date ‘dd/mm/yyyy’,
ENTIRE_LINE
)
demo1.dat
10,aaaab,Virginia,01/05/2001,This is the aaaab
Office in Virginia
20,aaaac,Virginia,13/04/2001,This is the aaaac
Office in Virginia
30,aaaad,Virginia,14/0

你可能感兴趣的:(oracle)