--參考鏈接:http://blog.itpub.net/10951282/viewspace-757712/
--ctl中默認記錄間的分割符是回車換行符,當記錄中含回車換行符時,可運用str屬性指定記錄分隔符:infile test.dat "str '|\r\n"
--設置字符集:UTF8、AL32UTF8、ZHS16GBK
--select * from v$nls_parameters;
--select name, value$ from sys.props$ where name like 'NLS%';
OPTIONS (skip=1,rows=128)
LOAD DATA
CHARACTERSET ZHS16GBK --即本行代碼為設置字符集
INFILE "D:\oracle\sqluldr2\users_data.csv"
INFILE "D:\oracle\sqluldr2\users_data1.csv"
INTO TABLE users_t TRUNCATE
FIELDS TERMINATED BY ","
--OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
virtual_column FILLER,
user_id,
user_name,
login_times,
last_login DATE "YYYY-MM-DD HH24:MI:SS"
)
--第二行還是導不進去,不知道為啥..連續分隔符?
--數據類型:DECIMAL EXTERNAL,INTEGER EXTERNAL,CHAR 不指定列類型時,默認為char
OPTIONS (skip=1,rows=128)
LOAD DATA
CHARACTERSET UTF8 --即本行代碼為設置字符集
INFILE *
INTO TABLE users_t TRUNCATE
FIELDS TERMINATED BY x'09'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
virtual_column FILLER,
user_id INTEGER EXTERNAL, --不加
user_name "case when length(:user_name)=0 then null else :user_name end", --必須用雙引號括起
login_times INTEGER EXTERNAL,
last_login DATE "YYYY-MM-DD HH24:MI:SS"
)
BEGINDATA
USER_ID USER_NAME LOGIN_TIMES LAST_LOGIN
1 1 Unmi 3 2020/11/16 17:00
2 2 5 2008/10/15
3 3 "隔葉 黃鶯" 8 2009/1/2
4 4 Kypfos
5 5 不知秋 1 2008/12/23
--infile后根文件名默認擴展名為'.dat'
--操作類型:replace, insert(默認), truncate, append
LOAD DATA
INFILE *
INTO TABLE dept1 REPLACE
FIELDS TERMINATED BY x'09'
(
deptno,
dname,
loc
)
BEGINDATA
10 sales Virginia
20 accounting Virginia
--處理定長數據
LOAD DATA
INFILE 'account.dat' --數據同控制文件在一起時,用infile *
INTO TABLE count_trans append
WHERE year='1990'
(
account_nbr position(01:10) character,
day position(11:12) character,
month position(13:14) character,
transation_code position(15:16) character,
credit_amount position(17:30) character
)
INTO TABLE count REPLACE
WHERE year>'1990'
(
account_nbr position(01:10) character,
day position(11:12) character,
month position(13:14) character,
transation_code position(15:16) character,
credit_amount position(17:30) character
)
--處理變長數據
--可為列單獨指定分隔符
--可以寫多個 INFILE "another_data_file.csv" 指定多個外部數據文件
--還可以使用 BADFILE、DISCARDFILE 來指定壞數據和丟棄數據的文件,
--infile 'accounts' discardfile mtidsc.rec badfile mthad.rec
--control選項:skip=跳過行數,rows=多少條提交一次(默認64),errors=允許的錯誤記錄數
--log=xx.log,bad=xx.bad,data=xxx(一般通過infile指定)
--log 記錄日志文件,默認控制文件名去掉ctl,加log
--bad 壞數據文件,默認控制文件名去掉ctl,加badOPTIONS (skip=1,rows=128)
LOAD DATA
INFILE 'customer.dat'
INTO TABLE aa append
WHERE year='1990'
(
customer_id char terminated by '',
status char terminated by '',
dsc_class char enclosed by '',
source char terminated by whitespace
)
--對列進行操作
LOAD DATA
INFILE *
INTO TABLE dept1 REPLACE
FIELDS TERMINATED BY ','
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated date 'dd/mm/yyyy'
)
BEGINDATA
10,sales,Virginia,1/5/20000
20,accounting,Virginia,21/6/1999
--trailing nullcols 字段沒有對應的值時允許為空
--字段拼接
LOAD DATA
INFILE *
INTO TABLE DEPT REPLACE
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated date 'dd/mm/yyyy',
enter_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
--條件賦值
LOAD DATA
INFILE *
INTO TABLE DEPT REPLACE
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "case when length(:last_updated)<=10 then to_date(:last_updated, 'dd/mm/yyyy')
else to_date(:last_updated, 'dd/mm/yyyy hh24:mi:ss') end"
)
BEGINDATA
10,Sales,Virginia,1/5/2000 12:03:03
20,Accounting,Virginia,21/6/1999
30,Consulting,Virginia,5/1/2000 01:23:00
40,Finance,Virginia,15/3/2001
--對於如下數據我們需自定義日期處理函數
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
--------
create or replace function my_to_date(p_string in varchar2) return date
as
type fmtArray is table or 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;
--可以指定filler列將其不導入數據庫
--Optionally enclosed by '"' 表示數據中含“”括起來的字段,比如該字段中含分隔符等
--oracle處理部分必須用雙引號括起來,oracle語法單引號表示字符串,防止沖突
LOAD DATA
INFILE *
INTO TABLE dept1 replace
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
deptno,
dname "upper(:dname)",
loc "upper(:loc)",
last_updated "case when length(:last_updated)<=10 then to_date(:last_updated, 'dd/mm/yyyy')
else to_date(:last_updated, 'dd/mm/yyyy hh24:mi:ss') end"
)
BEGINDATA
20,Something Not To BE Loaded,accounting,"Virginia,USA"
--sqlldr默認char類型輸入字節流最大長度為255,超過需指定 char(N),對於較短數據也最好指定長度
OPTIONS (skip=1,rows=128)
LOAD DATA
INFILE *
INTO TABLE users append
WHEN LOGIN_TIMES<>'8'
FIELDS TERMINATED BY ","
TRAILING NULLCOLS
(
virtual_column FILLER,
--prod_id char(32) "trim(:prod_id)",
--acc_num char(20) "replace(:acc_num,chr(13),'')"
user_id "user_seq.nextval", --這一列直接取序列的下一值,而不用數據中提供的值
user_name "'Hi '||upper(:user_name)", -- 還能用SQL函數或運算對數據進行加工處理
login_times terminated by "," NULLIF(login_times='NULL'), --可為列單獨指定分隔符
last_login DATE "YYYY-MM-DD HH24:MI:SS" NULLIF(last_login="NULL") -- 當字段為"NULL"時就是 NULL
)
BEGINDATA
,USER_ID,USER_NAME,LOGIN_TIMES,LAST_LOGIN
1,1,Unmi,3,2009-1-5 20:34
2,2,Fantasia,5,2008-10-15
3,3,隔葉黃鶯,8,2009-1-2
4,4,Kypfos,NULL,NULL
5,5,不知秋,1,2008-12-23
連接方式:
sqlldr user/pwd control=webaccess.ctl
sqlldr test/test@orcl control=D:\oracle\sqluldr2\users.ctl
sqlldr mh/mh@22.11.97.96:1521/ora10 control=fund_inf.ctl
--查看TERMINATED BY=x'09'和WHITESPACE的結果,WHITESPACE加載正常
LOAD DATA
INFILE *
INTO TABLE DEPT2
INSERT
FIELDS TERMINATED BY WHITESPACE
(DEPTNO, DNAME, LOC char(1000))
BEGINDATA
10 Sales Virginia
20 Accounting Virginia
30 Consulting Virginia
40 Finance Virginia
--也可以
LOAD DATA
INFILE *
INTO TABLE DEPT2
INSERT
FIELDS TERMINATED BY WHITESPACE
(DEPTNO, ad filler, DNAME, a2 filler, LOC char(1000))
BEGINDATA
10 Sales Virginia
20 Accounting Virginia
30 Consulting Virginia
40 Finance Virginia
--根據條件插入不同的表/分區,如一次裝載所有分區,可通過裝載表方式處理
into table account_trans
--into table sale partition(east_data)
when day between '01' and '31'
into table account_nbr
when account_type between 'aa' and 'zz'
--過濾開頭#號的行
INTO TABLE <TABLE_NAME>
WHEN (1) <> '#'