程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> sqlldr裝載數據完成代碼

sqlldr裝載數據完成代碼

編輯:MSSQL

sqlldr裝載數據完成代碼。本站提示廣大學習愛好者:(sqlldr裝載數據完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是sqlldr裝載數據完成代碼正文


這裡用 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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved