首页 > 代码库 > 『ORACLE』移动数据(Directory Object)

『ORACLE』移动数据(Directory Object)

 

SQL> create or replace directory dir_dt as ‘/home/oracle‘;

Directory created.
SQL> grant read,write on directory dir_dt to scott;

Grant succeeded.

 

SQL*Loader

测试一:infile *

常用参数

参数名称

含义
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 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 specificatiions
parallel 并行导入(默认false)

控制文件基本格式

load data

infile ‘t_01.dat‘          --要导入的数据文件名称

-- infile ‘t_02.dat‘      --如果是多个数据文件,那么可以在此处写入多个

--infile *                   --要导入的内容就在control文件里,begindata后面就是导入的内容(与上面格式不能同时使用)

 

insert:数据加载方式(默认)

加载方式有如下四种:

append:原先的表有数据就加在后面

insert:(默认值)装载空表,如果原先的表有数据sqlloader会停止

replace:原先的表有数据原先的数据会全部删除

truncate:制定的内容和replace的相同会用truncate语句删除现存数据

 

badfile ‘bf_name.bad‘:指定出现错误的记录存放的位置和名称。如果此参数没有指定,那么默认会在控制文件同目录下生成一个与存放数据的文件同名的且后缀为bad的文件

fields terminated by ‘,‘ optionally enclosed by ‘ ‘‘ ‘

转载的数据格式为,以‘,‘分隔的数据,且以‘ ‘‘ ‘来标识一个字段的起始。主要是因为,在平文本文件中,有可能出现带逗号的字段,那样,sqlloader会误以为,那个逗号为分隔符,导致load的数据是错误的。注:此参数可以声明也可以不声明,如果没声明,那么需要在定义字段的地方声明用什么来区分。

trailing nullcols:允许出现空值,当平文本文件中,没有对应表中字段的值,那么以null来代替。如果不加此参数,那么,对应不上的记录将无法写入表,会出现在bad文件中。

 

(col_name1,col_name2,col_name3):声明所有字段的名称。

如果没有声明fields terminated by ‘,‘,那么也可以在字段处进行声明,如下:

(col_name1 [interger external] terminated by‘,‘,

   col_name2 [date "DD-MON-YYYY] terminated by ‘,‘,

   col_name3 [char] terminated by ‘,‘ optionall enclosed by ‘ ‘‘ ‘)

如果没有声明fields terminated by ‘,‘,并且文本文件中没有任何的分隔标识,那么也可以用指定位置的方式来装载数据,如下:

(col_name1 position(1:2),

   col_name2 position(3:9),

   col_name3(*:15) char(8),//char(8)指定字段类型及长度,*:15,表示,从上一个字段结束的位置开始,15结束

   col_name4 position(16:30) "trim(:col_name4)",// 去掉本字段截取的字符两边的空格

  )

begindata:与infile*遥相呼应,即要导入的数据就在控制文件中,且在begindata的下面

-------------------------------------------------------------------------------------------------------------------------------

SQL*Loader: Release 11.2.0.4.0 - Production on Mon May 29 14:47:19 2017SQL> conn scott/tiger

Connected.
SQL> create table sl_base
  2      (id number(5),fname varchar2(10),lname varchar2(10));
Table created.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@enmo1 adump]$ cd
[oracle@enmo1 ~]$ vi base.ctl
load data
infile *
badfile ‘base.bad‘
into table sl_base
replace
fields terminated by ‘,‘
(id,fname,lname)
begindata
1,zhangfei,zhangyide
2,guanyu,guanyunchang
3,liubei,liuxuande
~
~
~
"base.ctl" [New] 12L, 182C written              
[oracle@enmo1 ~]$ sqlldr scott/tiger control = base.ctl

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
Commit point reached - logical record count 3

[oracle@enmo1 ~]$ cat base.bad 
2,guanyu,guanyunchang

SQL> conn scott/tiger
Connected.
SQL> select * from sl_base;
        ID FNAME      LNAME
---------- -------------- --------------
         1  zhangfei   zhangyide
         3 liubei         liuxuande

测试二:null值

 

[oracle@enmo1 ~]$ ls
afiedt.buf base.bad base.ctl base.log c.sql datadump rman
[oracle@enmo1 ~]$ rm base*
[oracle@enmo1 ~]$ ls
afiedt.buf c.sql datadump rman
[oracle@enmo1 ~]$ vi base_data.dat
1,zhangfei,zhangyide
2,guanyu,guanyunchang
3,liubei,liuxuande
4,kongming
~
~
"base_data.dat" [New] 4L, 73C written
[oracle@enmo1 ~]$
[oracle@enmo1 ~]$ vi base.ctl

 

load data
infile ‘base_data.dat‘
into table sl_base
truncate
fields terminated by ‘,‘
(id,fname,lname)
~
~
"base.ctl" [New] 7L, 104C written

[oracle@enmo1 ~]$ sqlldr scott/tiger control=base.ctl

 

SQL*Loader: Release 11.2.0.4.0 - Production on Mon May 29 16:32:08 2017

 

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.

 

Commit point reached - logical record count 4
[oracle@enmo1 ~]$ ll
total 36
-rw-r--r-- 1 oracle oinstall 71 May 24 20:09 afiedt.buf
-rw-r--r-- 1 oracle oinstall 104 May 29 16:26 base.ctl
-rw-r--r-- 1 oracle oinstall 33 May 29 16:32 base_data.bad
-rw-r--r-- 1 oracle oinstall 73 May 29 16:24 base_data.dat
-rw-r--r-- 1 oracle oinstall 1839 May 29 16:32 base.log
-rw-r--r-- 1 oracle oinstall 295 May 29 16:31 control-base.log
-rw-r--r-- 1 oracle oinstall 66 May 25 11:50 c.sql
drwxr-xr-x 2 oracle oinstall 4096 May 24 21:50 datadump
drwxr-xr-x 2 oracle oinstall 4096 May 28 11:57 rman

[oracle@enmo1 ~]$ cat base_data.bad
4,kongming
2,guanyu,guanyunchang

bad文件中有两条记录,id为2的记录未导入的原因是lname字段不够长,id为4的记录未导入原因是lname字段出现了空值

SQL> select * from sl_base;

ID FNAME LNAME
---------- ---------- ----------
1 zhangfei zhangyide
3 liubei liuxuande

解决null值的问题,编辑控制文件

[oracle@enmo1 ~]$ vi base.ctl

load data
infile ‘base_data.dat‘
into table sl_base
truncate
fields terminated by ‘,‘
trailing nullcols
(id,fname,lname)
~
~

[oracle@enmo1 ~]$ sqlldr scott/tiger control=base.ctl

SQL*Loader: Release 11.2.0.4.0 - Production on Mon May 29 16:39:37 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.

Commit point reached - logical record count 4

[oracle@enmo1 ~]$ cat base_data.bad
2,guanyu,guanyunchang

此时控制错误的记录已经能够不存在了

测试三:字符串中包含逗号

[oracle@enmo1 ~]$ rm base*
[oracle@enmo1 ~]$ ls
afiedt.buf control-base.log c.sql datadump rman
[oracle@enmo1 ~]$ vi base.ctl
load data
infile ‘base_data.dat‘
into table sl_base
truncate
trailling nullcols
(id terminated by ‘,‘,
fname terminated by ‘,‘,
lname terminated by ‘,‘optionally enclosed by ‘”‘
)
~
~

[oracle@enmo1 ~]$ rm base*
[oracle@enmo1 ~]$ ls
afiedt.buf control-base.log c.sql datadump rman

[oracle@enmo1 ~]$ vi base.ctl
load data
infile ‘base_data.dat‘
into table sl_base
truncate
trailing nullcols
(id terminated by ‘,‘,
fname terminated by ‘,‘,
lname terminated by ‘,‘optionally enclosed by ‘"‘
)
~
~
"base.ctl" 9L, 181C written
[oracle@enmo1 ~]$ sqlldr scott/tiger control=base.ctl

SQL*Loader: Release 11.2.0.4.0 - Production on Mon May 29 17:02:22 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.

Commit point reached - logical record count 6

[oracle@enmo1 ~]$ cat base_data.bad
2,guanyu,"guan,yunchang"

SQL> select * from sl_base;

ID FNAME LNAME
---------- ---------- ----------
1 zhangfei zhang,yide
3 liubei liu,xuande
4 kongming

测试四:数据文件中无分隔符

[oracle@enmo1 ~]$ rm base*

[oracle@enmo1 ~]$ vi base_data.dat

1zhangfeizhangyide
2guanyu guanyunchang
3liubei liuxuande
4kongming
~
~

[oracle@enmo1 ~]$ vi base.ctl

load data
infile ‘base_data.dat‘
into table sl_base
truncate
trailing nullcols
(id position(1:1),
fname position(2:9),
lname position(10:22)
)
~
~

[oracle@enmo1 ~]$ sqlldr scott/tiger control=base.ctl

SQL*Loader: Release 11.2.0.4.0 - Production on Mon May 29 17:12:25 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.

Commit point reached - logical record count 5

SQL> select * from sl_base;

ID FNAME LNAME
---------- ---------- ----------
1 zhangfei zhangyide
3 liubei l iuxuande
4 kongming

 

『ORACLE』移动数据(Directory Object)