Oracle SQL Loader的详细语法
轉(zhuǎn)載?http://www.cnblogs.com/binking/archive/2006/11/21/567587.html
Oracle?? SQL?? Loader的詳細(xì)語法? SQL*LOADER是ORACLE的數(shù)據(jù)加載工具,通常用來將操作系統(tǒng)文件遷移到ORACLE數(shù)據(jù)庫中。SQL*LOADER是大型數(shù)據(jù)
??倉庫選擇使用的加載方法,因為它提供了最快速的途徑(DIRECT,PARALLEL)。現(xiàn)在,我們拋開其理論不談,用實例來使您快速掌握SQL*LOADER的使用方法。
? ?首先,我們認(rèn)識一下SQL*LOADER。
? ?在NT下,SQL*LOADER的命令為SQLLDR,在UNIX下一般為sqlldr/sqlload。
? ?如執(zhí)行:d:\oracle>sqlldr
? SQL*Loader:?? Release?? 8.1.6.0.0?? -?? Production?? on???星期二1月8?? 11:06:42?? 2002???
? (c)?? Copyright?? 1999?? Oracle?? Corporation.?? All?? rights?? reserved.???
??用法:?? SQLLOAD???關(guān)鍵字=???值[,keyword=value,...]???
??有效的關(guān)鍵字:???
? userid?? --?? ORACLE?? username/password???
? control?? --?? Control?? file?? name???
? log?? --?? Log?? file?? name???
? bad?? --?? Bad?? file?? name???
? data?? --?? Data?? file?? name???
? discard?? --?? Discard?? file?? name???
? discardmax?? --?? Number?? of?? discards?? to?? allow?? (全部默認(rèn))???
? skip?? --?? Number?? of?? logical?? records?? to?? skip?? (默認(rèn)0)???
? load?? --?? Number?? of?? logical?? records?? to?? load?? (全部默認(rèn))???
? errors?? --?? Number?? of?? errors?? to?? allow?? (默認(rèn)50)???
? rows?? --?? Number?? of?? rows?? in?? conventional?? path?? bind?? array?? or?? between?? direct?? path?? data?? saves???
??(默認(rèn):???常規(guī)路徑64,???所有直接路徑)
? bindsize?? --?? Size?? of?? conventional?? path?? bind?? array?? in?? bytes(默認(rèn)65536)???
? silent?? --?? Suppress?? messages?? during?? run?? (header,feedback,errors,discards,partitions)???
? direct?? --?? use?? direct?? path?? (默認(rèn)FALSE)???
? parfile?? --?? parameter?? file:?? name?? of?? file?? that?? contains?? parameter?? specifications???
? parallel?? --?? do?? parallel?? load?? (默認(rèn)FALSE)???
? file?? --?? File?? to?? allocate?? extents?? from???
? skip_unusable_indexes?? --?? disallow/allow?? unusable?? indexes?? or?? index?? partitions(默認(rèn)FALSE)???
? skip_index_maintenance?? --?? do?? not?? maintain?? indexes,?? mark?? affected?? indexes?? as?? unusable(默認(rèn)FALSE)???
? commit_discontinued?? --?? commit?? loaded?? rows?? when?? load?? is?? discontinued(默認(rèn)FALSE)???
? readsize?? --?? Size?? of?? Read?? buffer?? (默認(rèn)1048576)
? PLEASE?? NOTE:???命令行參數(shù)可以由位置或關(guān)鍵字指定
??。前者的例子是'sqlload??scott/tiger?? foo';后者的例子是'sqlload?? control=foo userid=scott/tiger'.位置指定參數(shù)的時間必須早于但不可遲于由關(guān)鍵字指定的參數(shù)。例如,???
? 'SQLLOAD?? SCott/tiger?? control=foo?? logfile=log',???但'不允許 sqlload?? scott/tiger?? control=foo?? log',即使允許?參數(shù)'log'???的位置正確。
? d:\oracle>???
??我們可以從中看到一些基本的幫助信息,這里,我用到的是中文的WIN2000ADVSERVER。
??我們知道,SQL*LOADER只能導(dǎo)入純文本,所以我們現(xiàn)在開始以實例來講解其用法。
??一、已存在數(shù)據(jù)源result.csv,欲倒入ORACLE中FANCY用戶下。
??result.csv內(nèi)容:
??1,默認(rèn)Web???站點,192.168.2.254:80:,RUNNING???
??2,other,192.168.2.254:80:test.com,STOPPED???
??3,third,192.168.2.254:81:thirdabc.com,RUNNING???
??從中,我們看出4列,分別以逗號分隔,為變長字符串。
??二、制定控制文件result.ctl???
? result.ctl內(nèi)容:
??load?? data???
? infile?? 'result.csv'???
? into?? table?? resultxt?????
? (resultid?? char?? terminated?? by?? ',',???
? website?? char?? terminated?? by?? ',',???
? ipport?? char?? terminated?? by?? ',',???
? status?? char?? terminated?? by?? whitespace)???
??說明:
??infile指數(shù)據(jù)源文件 這里我們省略了默認(rèn)的 discardfile?? result.dsc?? badfile?? result.bad???
??into?? table?? resultxt???默認(rèn)是INSERT,也可以into?? table?? resultxt?? APPEND為追加方式,或REPLACE???
??terminated?? by?? ','指用逗號分隔
??terminated?? by?? whitespace結(jié)尾以空白分隔
??三、此時我們執(zhí)行加載:
??D:\>sqlldr?? userid=fancy/testpass?? control=result.ctl?? log=resulthis.out???
? SQL*Loader:?? Release?? 8.1.6.0.0?? -?? Production?? on???星期二1月8?? 10:25:42?? 2002???
? (c)?? Copyright?? 1999?? Oracle?? Corporation.?? All?? rights?? reserved.???
? SQL*Loader-941:???在描述表RESULTXT時出現(xiàn)錯誤
? ORA-04043:???對象RESULTXT???不存在
??提示出錯,因為數(shù)據(jù)庫沒有對應(yīng)的表。
??四、在數(shù)據(jù)庫建立表
??create?? table?? resultxt???
? (resultid?? varchar2(500),???
? website?? varchar2(500),???
? ipport?? varchar2(500),???
? status?? varchar2(500))???
? /???
?五、重新執(zhí)行加載
??D:\>sqlldr?? userid=fancy/k1i7l6l8?? control=result.ctl?? log=resulthis.out???
? SQL*Loader:?? Release?? 8.1.6.0.0?? -?? Production?? on???星期二1月8?? 10:31:57?? 2002???
? (c)?? Copyright?? 1999?? Oracle?? Corporation.?? All?? rights?? reserved.???
??達(dá)到提交點,邏輯記錄計數(shù)2???
??達(dá)到提交點,邏輯記錄計數(shù)3???
??已經(jīng)成功!我們可以通過日志文件來分析其過程:resulthis.out內(nèi)容如下:
? SQL*Loader:?? Release?? 8.1.6.0.0?? -?? Production?? on???星期二1月8?? 10:31:57?? 2002???
? (c)?? Copyright?? 1999?? Oracle?? Corporation.?? All?? rights?? reserved.???
??控制文件:?? result.ctl???
??數(shù)據(jù)文件:?? result.csv???
??錯誤文件:?? result.bad???
??廢棄文件:???未作指定
? :?????
? (可廢棄所有記錄)???
??裝載數(shù):?? ALL???
??跳過數(shù):?? 0???
??允許的錯誤:?? 50???
??綁定數(shù)組:?? 64???行,最大65536???字節(jié)
??繼續(xù):???未作指定
??所用路徑:???常規(guī)
??表RESULTXT???
??已載入從每個邏輯記錄
??插入選項對此表INSERT生效
??列名位置長度中止包裝數(shù)據(jù)類型
? ------------------------------?? ----------?? -----?? ----?? ----?? ---------------------???
? RESULTID?? FIRST?? *?? ,?? CHARACTER?????
? WEBSITE?? NEXT?? *?? ,?? CHARACTER?????
? IPPORT?? NEXT?? *?? ,?? CHARACTER?????
? STATUS?? NEXT?? *?? WHT?? CHARACTER?????
??表RESULTXT:?????
? 3???行載入成功
??由于數(shù)據(jù)錯誤,?? 0???行沒有載入。
??由于所有WHEN???子句失敗,?? 0???行沒有載入。
??由于所有字段都為空的,?? 0???行沒有載入。
??為結(jié)合數(shù)組分配的空間:?? 65016字節(jié)(63行)
??除綁定數(shù)組外的內(nèi)存空間分配:?? 0字節(jié)
??跳過的邏輯記錄總數(shù):?? 0???
??讀取的邏輯記錄總數(shù):?? 3???
??拒絕的邏輯記錄總數(shù):?? 0???
??廢棄的邏輯記錄總數(shù):?? 0???
??從星期二1月08?? 10:31:57?? 2002開始運行
??在星期二1月08?? 10:32:00?? 2002處運行結(jié)束
??經(jīng)過時間為:?? 00:?? 00:?? 02.70???
? CPU???時間為:?? 00:?? 00:?? 00.10(可
??六、并發(fā)操作
? sqlldr?? userid=/?? control=result1.ctl?? direct=true?? parallel=true???
? sqlldr?? userid=/?? control=result2.ctl?? direct=true?? parallel=true???
? sqlldr?? userid=/?? control=result2.ctl?? direct=true?? parallel=true???
??當(dāng)加載大量數(shù)據(jù)時(大約超過10GB),最好抑制日志的產(chǎn)生:
??SQL>ALTER?? TABLE?? RESULTXT?? nologging;???
??這樣不產(chǎn)生REDOLOG,可以提高效率。然后在CONTROL文件中l(wèi)oad?? data上面加一行:unrecoverable?????此選項必須要與DIRECT共同應(yīng)用。
??在并發(fā)操作時,ORACLE聲稱可以達(dá)到每小時處理100GB數(shù)據(jù)的能力!其實,估計能到1-10G就算不錯了,開始可用結(jié)構(gòu)?相同的文件,但只有少量數(shù)據(jù),成功后開始加載大量數(shù)據(jù),這樣可以避免時間的浪費。
??有關(guān)SQLLDR的問題
??控制文件:input.ctl,內(nèi)容如下:
??load?? data--1、控制文件標(biāo)識
??infile?? 'test.txt'--2、要輸入的數(shù)據(jù)文件名為test.txt?????
??append?? into?? table?? test--3、向表test中追加記錄
??fields?? terminated?? by?? X'09'--4、字段終止于X'09',是一個制表符(TAB)
??(id,username,password,sj)-----定義列對應(yīng)順序
??其中append為數(shù)據(jù)裝載方式,還有其他選項:
? a、insert,為缺省方式,在數(shù)據(jù)裝載開始時要求表為空
? b、append,在表中追加新記錄
? c、replace,刪除舊記錄,替換成新裝載的記錄
? d、truncate,同上 ================================================================================== PS:????下面是我用C#寫的調(diào)用SQL_LOADER的方法 Private Function Exec_SqlLoader(ByVal s_aFilePath As String, ByVal o_afileName As Hashtable, ByRef s_aErrMsg As String) As Boolean
??????? FilePath = s_aFilePath.Trim("\")
??????? Dim ctlPath As String = FilePath + "\control"
??????? s_aErrMsg = ""
??????? Try
??????????? For i As Integer = 1 To 4
??????????????? Dim strmReader As New StreamReader(ctlPath + "\001" + i.ToString() + ".ctl")
??????????????? Dim ctlContent As String = strmReader.ReadToEnd()
??????????????? ctlContent = ctlContent.Replace("{{dataFile}}", o_afileName.Item(i.ToString))
??????????????? strmReader.Close()
??????????????? strmReader = Nothing
??????????????? Dim strCtlName As String = ctlPath + "\001" + i.ToString() + "new.ctl"
??????????????? Dim strLogName As String = ctlPath + "\001" + i.ToString() + "new.log"
??????????????? Dim strBadName As String = ctlPath + "\001" + i.ToString() + "new.bad"
??????????????? Dim strDscName As String = ctlPath + "\001" + i.ToString() + "new.dsc"
??????????????? Dim strmWriter As StreamWriter = File.CreateText(strCtlName)
??????????????? strmWriter.Write(ctlContent)
??????????????? strmWriter.Close()
??????????????? strmWriter = Nothing If (File.Exists(strBadName)) Then
??????????????????? File.Delete(strBadName)
??????????????? End If Dim connSetting As String() = thisVersion.MSD_ConnectionStr.Split(";")
??????????????? Dim serviceName As String = CType(connSetting.GetValue(0), String)
??????????????? serviceName = serviceName.Substring(serviceName.IndexOf("=") + 1)
??????????????? Dim userId As String = CType(connSetting.GetValue(1), String)
??????????????? userId = userId.Substring(userId.IndexOf("=") + 1)
??????????????? Dim password As String = CType(connSetting.GetValue(2), String)
??????????????? password = password.Substring(password.IndexOf("=") + 1) Dim strPathName As String
??????????????? strPathName = "sqlldr " + userId + "/" + password + "@" + serviceName + " control='" + strCtlName + "' log='" + strLogName + "' bad='" + strBadName + "' discard='" + strDscName + "' errors=500"
??????????????? Microsoft.VisualBasic.Shell(strPathName, AppWinStyle.MinimizedFocus, True) If (File.Exists(strBadName)) Then
??????????????????? File.Copy(strBadName, FilePath + "\BatchLog\ERROR_001" + i.ToString() + ".txt", True)
??????????????????? a_sErrMsg +=?"這里寫拋出的警告信息" + vbCrLf + vbTab
????????????????????
??????????????? End If
??????????? Next
????????????
??????????? Return True
??????? Catch ex As Exception
??????????? a_sErrMsg += ex.ToString()
??????????? ms_BatchLogMsg = ex.Message?
??????????? Return False
??????? End Try
??? End Function ---
使用Oracle的sqlldr工具來將SDSS.CSV文件中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫。
SDSS.CSV 放置于 E:\ 內(nèi)容如下:
??????? 2.3757291096910399E-4,-8.9490277843631194,20.302597,
??????? 0.00023851549291556,-0.88450365314434698,20.098101,
??????? 2.5168890447969299E-4,7.7169029946908094E-2,18.762859,
??????? 2.9486118387225802E-4,-9.8447540892170409,19.943598,
??????? 2.94865362739442E-4,-9.8447540642891909,19.944597,
操作步驟:
1、在Oracle中按照導(dǎo)入數(shù)據(jù)的格式建立一個空表
???????????????????? 類型???????? 大小??? 小數(shù)
??????? RA??????? Number??? 23?????? 20
??????? DEC?????Number??? 23?????? 20
??????? MAG???? Number??? 8???????? 6
2、編寫一個loader.ctl文件,內(nèi)容如下
??????? load data?
??????? infile 'E:\SDSS.csv'?
??????? into table SDSS_STARS?
??????? fields terminated by ","
??????? ( RA, DEC, MAG )
3、在CMD中執(zhí)行以下語句:
????????sqlldr scott/tiger control=loader.ctl
4、自動在當(dāng)前目錄下生成兩個文件
??????? 默認(rèn)日志文件名為:loader.log?
??????? 默認(rèn)壞記錄文件為:SDSS.bad
注意事項:
1、必須在服務(wù)器本機(jī)上操作,不能在只安裝Oracle客戶端的機(jī)器上使用。
2、CSV文件的內(nèi)容是以“,“分隔的,如果數(shù)據(jù)在結(jié)尾沒有逗號導(dǎo)入時就會出錯。
可以利用UltraEdit等文本編輯器處理一下,將 ^p 替換為 ,^p 就可以在每行的末尾加上一個逗號了。
3、數(shù)據(jù)導(dǎo)入需要較長的時間。
總結(jié)
以上是生活随笔為你收集整理的Oracle SQL Loader的详细语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何修改Oracle并发连接数的设置
- 下一篇: 查询 缓存