sqlldr装载数据实现代码
程序员文章站
2023-11-29 09:57:58
这里用 excel 数据做个最简单的测试。 1)把 excel 数据存为 t.txt 文件,注意文件后缀名为 .txt 1 jhchen 11/07/2005 20:04:...
这里用 excel 数据做个最简单的测试。
1)把 excel 数据存为 t.txt 文件,注意文件后缀名为 .txt
1 jhchen 11/07/2005 20:04:00 2005-11-7 20:04
2 jhchen 11/07/2005 20:04:00 2005-11-7 20:04
3 jhchen 11/07/2005 20:04:00 2005-11-7 20:04
2)sql> create table t_load(
id number,
name varchar2(10),
dat1 date,
dat2 date,
dat3 date
);
table created.
3)控制文件 t.ctl 如下
load data
infile 't.txt'
badfile 't.bad'
append into table t_load
fields terminated by x'09'
trailing nullcols
(
id ,
name ,
dat1 date "mm/dd/yyyy hh24:mi:ss",
dat2 date "yyyy-mm-dd hh24:mi:ss"
)
其中,x'09' 是制表符,tab键,trailing nullcols 表示如表的字段没有对应的值时允许为空。
4)c:\documents and settings\cjh>sqlldr userid=jhchen/oracle control=t.ctl
sql*loader: release 9.2.0.6.0 - production on 星期一 11月 7 20:20:00 2005
copyright (c) 1982, 2002, oracle corporation. all rights reserved.
达到提交点,逻辑记录计数3
5)sql> select * from t_load;
id name dat1 dat2 dat3
---------- ---------- --------- --------- ---------
1 jhchen 07-nov-05 07-nov-05
2 jhchen 07-nov-05 07-nov-05
3 jhchen 07-nov-05 07-nov-05
sqlldr userid=lgone/tiger control=a.ctl
load data
infile 't.dat' // 要导入的文件
// infile 'tt.date' // 导入多个文件
// infile * // 要导入的内容就在control文件里 下面的begindata后面就是导入的内容
into table table_name // 指定装入的表
badfile 'c:\bad.txt' // 指定坏文件地址
************* 以下是4种装入表的方式
append // 原先的表有数据 就加在后面
// insert // 装载空表 如果原先的表有数据 sqlloader会停止 默认值
// replace // 原先的表有数据 原先的数据会全部删除
// truncate // 指定的内容和replace的相同 会用truncate语句删除现存数据
************* 指定的terminated可以在表的开头 也可在表的内部字段部分
fields terminated by ',' optionally enclosed by '"'
// 装载这种数据: 10,lg,"""lg""","lg,lg"
// 在表中结果: 10 lg "lg" lg,lg
// terminated by x '09' // 以十六进制格式 '09' 表示的
// terminated by writespace // 装载这种数据: 10 lg lg
trailing nullcols ************* 表的字段没有对应的值时允许为空
************* 下面是表的字段
(
col_1 , col_2 ,col_filler filler // filler 关键字 此列的数值不会被装载
// 如: lg,lg,not 结果 lg lg
)
// 当没声明fields terminated by ',' 时
// (
// col_1 [interger external] terminated by ',' ,
// col_2 [date "dd-mon-yyy"] terminated by ',' ,
// col_3 [char] terminated by ',' optionally enclosed by 'lg'
// )
// 当没声明fields terminated by ','用位置告诉字段装载数据
// (
// col_1 position(1:2),
// col_2 position(3:10),
// col_3 position(*:16), // 这个字段的开始位置在前一字段的结束位置
// col_4 position(1:16),
// col_5 position(3:10) char(8) // 指定字段的类型
// )
begindata // 对应开始的 infile * 要导入的内容就在control文件里
10,sql,what
20,lg,show
=====================================================================================
//////////// 注意begindata后的数值前面不能有空格
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 列将为空
2 ***** fields terminated by whitespace 和 fields terminated by x'09' 的情况
load data
infile *
into table dept
replace
fields terminated by whitespace
-- fields terminated by x'09'
(deptno,
dname,
loc
)
begindata
10 sales virginia
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
5 ***** 使用函数 日期的一种表达 trailing nullcols的使用
load data
infile *
into table dept
replace
fields terminated by ','
trailing nullcols // 其实下面的entire_line在begindata后面的数据中是没有直接对应
// 的列的值的 如果第一行改为 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
6 ***** 使用自定义的函数 // 解决的时间问题
create or replace
function my_to_date( p_string in varchar2 ) return date
as
type fmtarray is table of varchar2(25);
l_fmts fmtarray := fmtarray( 'dd-mon-yyyy', 'dd-month-yyyy',
'dd/mm/yyyy',
'dd/mm/yyyy hh24:mi:ss' );
l_return date;
begin
for i in 1 .. l_fmts.count
loop
begin
l_return := to_date( p_string, l_fmts(i) );
exception
when others then null;
end;
exit when l_return is not null;
end loop;
if ( l_return is null )
then
l_return :=
new_time( to_date('01011970','ddmmyyyy') + 1/24/60/60 *
p_string, 'gmt', 'est' );
end if;
return l_return;
end;
/
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 )" // 使用自定义的函数
)
begindata
10,sales,virginia,01-april-2001
20,accounting,virginia,13/04/2001
30,consulting,virginia,14/04/2001 12:02:02
40,finance,virginia,987268297
50,finance,virginia,02-apr-2001
60,finance,virginia,not a date
7 ***** 合并多行记录为一行记录
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, // 其实这3行看成一行 10,sales,virginia,1/5/2000
virginia,
1/5/2000
// 这列子用 continueif list="," 也可以
告诉sqlldr在每行的末尾找逗号 找到逗号就把下一行附加到上一行
load data
infile *
continueif this(1:1) = '-' // 找每行的开始是否有连接字符 - 有就把下一行连接为一行
// 如 -10,sales,virginia,
// 1/5/2000 就是一行 10,sales,virginia,1/5/2000
// 其中1:1 表示从第一行开始 并在第一行结束 还有continueif next 但continueif list最理想
into table dept
replace
fields terminated by ','
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated date 'dd/mm/yyyy'
)
begindata // 但是好象不能象右面的那样使用
-10,sales,virginia, -10,sales,virginia,
1/5/2000 1/5/2000
-40, 40,finance,virginia,13/04/2001
finance,virginia,13/04/2001
8 ***** 载入每行的行号
load data
infile *
into table t
replace
( seqno recnum //载入每行的行号
text position(1:1024))
begindata
fsdfasj //自动分配一行号给载入 表t 的seqno字段 此行为 1
fasdjfasdfl // 此行为 2 ...
9 ***** 载入有换行符的数据
注意: unix 和 windows 不同 \\n & /n
< 1 > 使用一个非换行符的字符
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,'\n',chr(10))" // replace 的使用帮助转换换行符
)
begindata
10,sales,virginia,01-april-2001,this is the sales\noffice in virginia
20,accounting,virginia,13/04/2001,this is the accounting\noffice in virginia
30,consulting,virginia,14/04/2001 12:02:02,this is the consulting\noffice in virginia
40,finance,virginia,987268297,this is the finance\noffice in virginia
< 2 > 使用fix属性
load data
infile demo17.dat "fix 101"
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo17.dat
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
// 这样装载会把换行符装入数据库 下面的方法就不会 但要求数据的格式不同
load data
infile demo18.dat "fix 101"
into table dept
replace
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo18.dat
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"
< 3 > 使用var属性
load data
infile demo19.dat "var 3"
// 3 告诉每个记录的前3个字节表示记录的长度 如第一个记录的 071 表示此记录有 71 个字节
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo19.dat
07110,sales,virginia,01-april-2001,this is the sales
office in virginia
07820,accounting,virginia,13/04/2001,this is the accounting
office in virginia
08730,consulting,virginia,14/04/2001 12:02:02,this is the consulting
office in virginia
07140,finance,virginia,987268297,this is the finance
office in virginia
< 4 > 使用str属性
// 最灵活的一中 可定义一个新的行结尾符 win 回车换行 : chr(13)||chr(10)
此列中记录是以 a|\r\n 结束的
select utl_raw.cast_to_raw('|'||chr(13)||chr(10)) from dual;
结果 7c0d0a
load data
infile demo20.dat "str x'7c0d0a'"
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo20.dat
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|
==============================================================================
象这样的数据 用 nullif 子句
10-jan-200002350flipper seemed unusually hungry today.
10510-jan-200009945spread over three meals.
id position(1:3) nullif // 这里可以是blanks 或者别的表达式
// 下面是另一个列子 第一行的 1 在数据库中将成为 null
load data
infile *
into table t
replace
(n position(1:2) integer external nullif n='1',
v position(3:8)
)
begindata
1 10
20lg
1)把 excel 数据存为 t.txt 文件,注意文件后缀名为 .txt
1 jhchen 11/07/2005 20:04:00 2005-11-7 20:04
2 jhchen 11/07/2005 20:04:00 2005-11-7 20:04
3 jhchen 11/07/2005 20:04:00 2005-11-7 20:04
2)sql> create table t_load(
id number,
name varchar2(10),
dat1 date,
dat2 date,
dat3 date
);
table created.
3)控制文件 t.ctl 如下
load data
infile 't.txt'
badfile 't.bad'
append into table t_load
fields terminated by x'09'
trailing nullcols
(
id ,
name ,
dat1 date "mm/dd/yyyy hh24:mi:ss",
dat2 date "yyyy-mm-dd hh24:mi:ss"
)
其中,x'09' 是制表符,tab键,trailing nullcols 表示如表的字段没有对应的值时允许为空。
4)c:\documents and settings\cjh>sqlldr userid=jhchen/oracle control=t.ctl
sql*loader: release 9.2.0.6.0 - production on 星期一 11月 7 20:20:00 2005
copyright (c) 1982, 2002, oracle corporation. all rights reserved.
达到提交点,逻辑记录计数3
5)sql> select * from t_load;
id name dat1 dat2 dat3
---------- ---------- --------- --------- ---------
1 jhchen 07-nov-05 07-nov-05
2 jhchen 07-nov-05 07-nov-05
3 jhchen 07-nov-05 07-nov-05
sqlldr userid=lgone/tiger control=a.ctl
load data
infile 't.dat' // 要导入的文件
// infile 'tt.date' // 导入多个文件
// infile * // 要导入的内容就在control文件里 下面的begindata后面就是导入的内容
into table table_name // 指定装入的表
badfile 'c:\bad.txt' // 指定坏文件地址
************* 以下是4种装入表的方式
append // 原先的表有数据 就加在后面
// insert // 装载空表 如果原先的表有数据 sqlloader会停止 默认值
// replace // 原先的表有数据 原先的数据会全部删除
// truncate // 指定的内容和replace的相同 会用truncate语句删除现存数据
************* 指定的terminated可以在表的开头 也可在表的内部字段部分
fields terminated by ',' optionally enclosed by '"'
// 装载这种数据: 10,lg,"""lg""","lg,lg"
// 在表中结果: 10 lg "lg" lg,lg
// terminated by x '09' // 以十六进制格式 '09' 表示的
// terminated by writespace // 装载这种数据: 10 lg lg
trailing nullcols ************* 表的字段没有对应的值时允许为空
************* 下面是表的字段
(
col_1 , col_2 ,col_filler filler // filler 关键字 此列的数值不会被装载
// 如: lg,lg,not 结果 lg lg
)
// 当没声明fields terminated by ',' 时
// (
// col_1 [interger external] terminated by ',' ,
// col_2 [date "dd-mon-yyy"] terminated by ',' ,
// col_3 [char] terminated by ',' optionally enclosed by 'lg'
// )
// 当没声明fields terminated by ','用位置告诉字段装载数据
// (
// col_1 position(1:2),
// col_2 position(3:10),
// col_3 position(*:16), // 这个字段的开始位置在前一字段的结束位置
// col_4 position(1:16),
// col_5 position(3:10) char(8) // 指定字段的类型
// )
begindata // 对应开始的 infile * 要导入的内容就在control文件里
10,sql,what
20,lg,show
=====================================================================================
//////////// 注意begindata后的数值前面不能有空格
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 列将为空
2 ***** fields terminated by whitespace 和 fields terminated by x'09' 的情况
load data
infile *
into table dept
replace
fields terminated by whitespace
-- fields terminated by x'09'
(deptno,
dname,
loc
)
begindata
10 sales virginia
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
5 ***** 使用函数 日期的一种表达 trailing nullcols的使用
load data
infile *
into table dept
replace
fields terminated by ','
trailing nullcols // 其实下面的entire_line在begindata后面的数据中是没有直接对应
// 的列的值的 如果第一行改为 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
6 ***** 使用自定义的函数 // 解决的时间问题
create or replace
function my_to_date( p_string in varchar2 ) return date
as
type fmtarray is table of varchar2(25);
l_fmts fmtarray := fmtarray( 'dd-mon-yyyy', 'dd-month-yyyy',
'dd/mm/yyyy',
'dd/mm/yyyy hh24:mi:ss' );
l_return date;
begin
for i in 1 .. l_fmts.count
loop
begin
l_return := to_date( p_string, l_fmts(i) );
exception
when others then null;
end;
exit when l_return is not null;
end loop;
if ( l_return is null )
then
l_return :=
new_time( to_date('01011970','ddmmyyyy') + 1/24/60/60 *
p_string, 'gmt', 'est' );
end if;
return l_return;
end;
/
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 )" // 使用自定义的函数
)
begindata
10,sales,virginia,01-april-2001
20,accounting,virginia,13/04/2001
30,consulting,virginia,14/04/2001 12:02:02
40,finance,virginia,987268297
50,finance,virginia,02-apr-2001
60,finance,virginia,not a date
7 ***** 合并多行记录为一行记录
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, // 其实这3行看成一行 10,sales,virginia,1/5/2000
virginia,
1/5/2000
// 这列子用 continueif list="," 也可以
告诉sqlldr在每行的末尾找逗号 找到逗号就把下一行附加到上一行
load data
infile *
continueif this(1:1) = '-' // 找每行的开始是否有连接字符 - 有就把下一行连接为一行
// 如 -10,sales,virginia,
// 1/5/2000 就是一行 10,sales,virginia,1/5/2000
// 其中1:1 表示从第一行开始 并在第一行结束 还有continueif next 但continueif list最理想
into table dept
replace
fields terminated by ','
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated date 'dd/mm/yyyy'
)
begindata // 但是好象不能象右面的那样使用
-10,sales,virginia, -10,sales,virginia,
1/5/2000 1/5/2000
-40, 40,finance,virginia,13/04/2001
finance,virginia,13/04/2001
8 ***** 载入每行的行号
load data
infile *
into table t
replace
( seqno recnum //载入每行的行号
text position(1:1024))
begindata
fsdfasj //自动分配一行号给载入 表t 的seqno字段 此行为 1
fasdjfasdfl // 此行为 2 ...
9 ***** 载入有换行符的数据
注意: unix 和 windows 不同 \\n & /n
< 1 > 使用一个非换行符的字符
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,'\n',chr(10))" // replace 的使用帮助转换换行符
)
begindata
10,sales,virginia,01-april-2001,this is the sales\noffice in virginia
20,accounting,virginia,13/04/2001,this is the accounting\noffice in virginia
30,consulting,virginia,14/04/2001 12:02:02,this is the consulting\noffice in virginia
40,finance,virginia,987268297,this is the finance\noffice in virginia
< 2 > 使用fix属性
load data
infile demo17.dat "fix 101"
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo17.dat
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
// 这样装载会把换行符装入数据库 下面的方法就不会 但要求数据的格式不同
load data
infile demo18.dat "fix 101"
into table dept
replace
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo18.dat
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"
< 3 > 使用var属性
load data
infile demo19.dat "var 3"
// 3 告诉每个记录的前3个字节表示记录的长度 如第一个记录的 071 表示此记录有 71 个字节
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo19.dat
07110,sales,virginia,01-april-2001,this is the sales
office in virginia
07820,accounting,virginia,13/04/2001,this is the accounting
office in virginia
08730,consulting,virginia,14/04/2001 12:02:02,this is the consulting
office in virginia
07140,finance,virginia,987268297,this is the finance
office in virginia
< 4 > 使用str属性
// 最灵活的一中 可定义一个新的行结尾符 win 回车换行 : chr(13)||chr(10)
此列中记录是以 a|\r\n 结束的
select utl_raw.cast_to_raw('|'||chr(13)||chr(10)) from dual;
结果 7c0d0a
load data
infile demo20.dat "str x'7c0d0a'"
into table dept
replace
fields terminated by ','
trailing nullcols
(deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "my_to_date( :last_updated )",
comments
)
demo20.dat
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|
==============================================================================
象这样的数据 用 nullif 子句
10-jan-200002350flipper seemed unusually hungry today.
10510-jan-200009945spread over three meals.
id position(1:3) nullif // 这里可以是blanks 或者别的表达式
// 下面是另一个列子 第一行的 1 在数据库中将成为 null
load data
infile *
into table t
replace
(n position(1:2) integer external nullif n='1',
v position(3:8)
)
begindata
1 10
20lg