『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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章