oracle sql loader全攻略

Oracle   SQL   Loader 的详细语法    
  SQL*LOADER
ORACLE 的数据加载工具,通常用来将操作系统文件迁移到 ORACLE 数据库中。 SQL*LOADER 是大型数据    
  
仓库选择使用的加载方法,因为它提供了最快速的途径( DIRECT PARALLEL )。现在,我们抛开其理论不谈,用实例来使您快速掌握 SQL*LOADER 的使用方法。    
   
首先,我们认识一下 SQL*LOADER    
   
NT 下, SQL*LOADER 的命令为 SQLLDR ,在 UNIX 下一般为 sqlldr/sqlload    
   
如执行: 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    关键字    =       [,keyword=value,...]   
  
有效的关键字 :   
  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   (
全部默认 )   
  skip   --   Number   of   logical   records   to   skip   (
默认 0)   
  load   --   Number   of   logical   records   to   load   (
全部默认 )   
  errors   --   Number   of   errors   to   allow   (
默认 50)   
  rows   --   Number   of   rows   in   conventional   path   bind   array   or   between   direct   path   data   saves   
  
(默认 :    常规路径    64,    所有直接路径)    
  bindsize   --   Size   of   conventional   path   bind   array   in   bytes(
默认 65536)   
  silent   --   Suppress   messages   during   run   (header,feedback,errors,discards,partitions)   
  direct   --   use   direct   path   (
默认 FALSE)   
  parfile   --   parameter   file:   name   of   file   that   contains   parameter   specifications   
  parallel   --   do   parallel   load   (
默认 FALSE)   
  file   --   File   to   allocate   extents   from   
  skip_unusable_indexes   --   disallow/allow   unusable   indexes   or   index   partitions(
默认 FALSE)   
  skip_index_maintenance   --   do   not   maintain   indexes,   mark   affected   indexes   as   unusable(
默认 FALSE)   
  commit_discontinued   --   commit   loaded   rows   when   load   is   discontinued(
默认 FALSE)   
  readsize   --   Size   of   Read   buffer   (
默认 1048576)    
  PLEASE   NOTE:   
命令行参数可以由位置或关键字指定    
  
。前者的例子是    'sqlload  scott/tiger   foo'; 后者的例子是    'sqlload   control=foo userid=scott/tiger'. 位置指定参数的时间必须早于但不可迟于由关键字指定的参数。例如 ,   
  'SQLLOAD   SCott/tiger   control=foo   logfile=log',   
' 不允许                              
sqlload   scott/tiger   control=foo   log', 即使允许  参数    'log'    的位置正确。    
  d:\oracle>   
  
我们可以从中看到一些基本的帮助信息,这里,我用到的是中文的 WIN2000   ADV   SERVER    
  
       我们知道, SQL*LOADER 只能导入纯文本,所以我们现在开始以实例来讲解其用法。    
  
       一、已存在数据源 result.csv ,欲倒入 ORACLE FANCY 用户下。    
  
        result.csv 内容:    
  
      1, 默认    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
内容:    
  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  指数据源文件 这里我们省略了默认的  discardfile   result.dsc   badfile   result.bad   
  
      into   table   resultxt    默认是 INSERT ,也可以 into   table   resultxt   APPEND 为追加方式,或 REPLACE   
  
      terminated   by   ','  指用逗号分隔    
  
      terminated   by   whitespace  结尾以空白分隔    
  
       三、此时我们执行加载:    
  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 时出现错误    
  ORA-04043:   
对象    RESULTXT    不存在    
  
       提示出错,因为数据库没有对应的表。    
  
       四、在数据库建立表    
  
     create   table   resultxt   
  (resultid   varchar2(500),   
  website   varchar2(500),   
  ipport   varchar2(500),   
  status   varchar2(500))   
  /   
         五、重新执行加载    
  
      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.   
  
达到提交点,逻辑记录计数 2   
  
达到提交点,逻辑记录计数 3   
  
       已经成功!我们可以通过日志文件来分析其过程: 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.   
  
控制文件 :   result.ctl   
  
数据文件 :   result.csv   
  
错误文件 :   result.bad   
  
废弃文件 :    未作指定    
  :     
  (
可废弃所有记录 )   
  
装载数 :   ALL   
  
跳过数 :   0   
  
允许的错误 :   50   
  
绑定数组 :   64    行,最大    65536    字节    
  
继续 :    未作指定    
  
所用路径 :    常规    
  
RESULTXT   
  
已载入从每个逻辑记录    
  
插入选项对此表 INSERT 生效    
  
列名     位置     长度     中止     包装数据类型    
  ------------------------------   ----------   -----   ----   ----   ---------------------   
  RESULTID   FIRST   *   ,   CHARACTER     
  WEBSITE   NEXT   *   ,   CHARACTER     
  IPPORT   NEXT   *   ,   CHARACTER     
  STATUS   NEXT   *   WHT   CHARACTER     
  
RESULTXT:     
  3   
行载入成功    
  
由于数据错误 ,   0    行没有载入。    
  
由于所有    WHEN    子句失败 ,   0    行没有载入。    
  
由于所有字段都为空的 ,   0    行没有载入。    
  
为结合数组分配的空间 :   65016 字节( 63 行)    
  
除绑定数组外的内存空间分配 :   0 字节    
  
跳过的逻辑记录总数 :   0   
  
读取的逻辑记录总数 :   3   
  
拒绝的逻辑记录总数 :   0   
  
废弃的逻辑记录总数 :   0   
  
从星期二    1    08   10:31:57   2002 开始运行    
  
在星期二    1    08   10:32:00   2002 处运行结束    
  
经过时间为 :   00:   00:   02.70   
  CPU   
时间为 :   00:   00:   00.10(    
  
       六、并发操作    
  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
   
  
当加载大量数据时(大约超过 10GB ),最好抑制日志的产生:    
  
      SQL>ALTER   TABLE   RESULTXT   nologging;   
  
这样不产生 REDO   LOG ,可以提高效率。然后在 CONTROL 文件中 load   data 上面加一行: unrecoverable      此选项必须要与 DIRECT 共同应用。    
  
 在并发操作时, ORACLE 声称可以达到每小时处理 100GB 数据的能力!其实,估计能到 1 10G 就算不错了,开始可用结构  相同的文件,但只有少量数据,成功后开始加载大量数据,这样可以避免时间的浪费。    
  
有关 SQLLDR 的问题      
  
控制文件: input.ctl ,内容如下:      
  
      load   data               --1 、控制文件标识      
  
      infile   'test.txt'           --2 、要输入的数据文件名为 test.txt     
  
      append   into   table   test      --3 、向表 test 中追加记录      
  
      fields   terminated   by   X'09'    --4 、字段终止于 X'09' ,是一个制表符( TAB      
  
      (id,username,password,sj)       ----- 定义列对应顺序      
    
  
其中 append 为数据装载方式,还有其他选项:      
  a
insert ,为缺省方式,在数据装载开始时要求表为空      
  b
append ,在表中追加新记录      
  c
replace ,删除旧记录,替换成新装载的记录      
  d
truncate ,同上       
==================================================================================
PS:     下面是我用 C# 写的调用 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


一:sql loader 的特点
oracle自己带了很多的工具可以用来进行数据的迁移、备份和恢复等工作。但是每个工具都有自己的特点。
比如说exp和imp可以对数据库中的数据进行导出和导出的工作,是一种很好的数据库备份和恢复的工具,因此主要用在数据库的热备份和恢复方面。有着速度快,使用简单,快捷的优点;同时也有一些缺点,比如在不同版本数据库之间的导出、导入的过程之中,总会出现这样或者那样的问题,这个也许是oracle公司自己产品的兼容性的问题吧。
sql loader 工具却没有这方面的问题,它可以把一些以文本格式存放的数据顺利的导入到oracle数据库中,是一种在不同数据库之间进行数据迁移的非常方便而且通用的工具。缺点就速度比较慢,另外对blob等类型的数据就有点麻烦了。

二:sql loader 的帮助

C:\>sqlldr

SQL*Loader: Release 9.2.0.1.0 - Production on 星期六 10月 9 14:48:12 2004

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.

用法: SQLLDR keyword=value [,keyword=value,...]

有效的关键字:

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 (全部默认)
skip -- Number of logical records to skip (默认0)
load -- Number of logical records to load (全部默认)
errors -- Number of errors to allow (默认50)
rows -- Number of rows in conventional path bind array or between direct p
ath data saves
(默认: 常规路径 64, 所有直接路径)
bindsize -- Size of conventional path bind array in bytes(默认256000)
silent -- Suppress messages during run (header,feedback,errors,discards,part
itions)
direct -- use direct path (默认FALSE)
parfile -- parameter file: name of file that contains parameter specification
s
parallel -- do parallel load (默认FALSE)
file -- File to allocate extents from
skip_unusable_indexes -- disallow/allow unusable indexes or index partitions(默
认FALSE)
skip_index_maintenance -- do not maintain indexes, mark affected indexes as unus
able(默认FALSE)
readsize -- Size of Read buffer (默认1048576)
external_table -- use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE(
默认NOT_USED)
columnarrayrows -- Number of rows for direct path column array(默认5000)
streamsize -- Size of direct path stream buffer in bytes(默认256000)
multithreading -- use multithreading in direct path
resumable -- enable or disable resumable for current session(默认FALSE)
resumable_name -- text string to help identify resumable statement
resumable_timeout -- wait time (in seconds) for RESUMABLE(默认7200)
date_cache -- size (in entries) of date conversion cache(默认1000)

PLEASE NOTE: 命令行参数可以由位置或关键字指定
。前者的例子是 'sqlload
scott/tiger foo'; 后一种情况的一个示例是 'sqlldr control=foo
userid=scott/tiger'.位置指定参数的时间必须早于
但不可迟于由关键字指定的参数。例如,
允许 'sqlldr scott/tiger control=foo logfile=log', 但是
不允许 'sqlldr scott/tiger control=foo log', 即使
参数 'log' 的位置正确。

C:\>

三:sql loader使用例子
a)SQLLoader将 Excel 数据导出到 Oracle
1.创建SQL*Loader输入数据所需要的文件,均保存到C:\,用记事本编辑: 
控制文件:input.ctl,内容如下: 

   load data           --1、控制文件标识 
   infile 'test.txt'       --2、要输入的数据文件名为test.txt 
   append into table test    --3、向表test中追加记录 
   fields terminated by X'09'  --4、字段终止于X'09',是一个制表符(TAB) 
   (id,username,password,sj)   -----定义列对应顺序 

a、insert,为缺省方式,在数据装载开始时要求表为空 
b、append,在表中追加新记录 
c、replace,删除旧记录,替换成新装载的记录 
d、truncate,同上 

在DOS窗口下使用SQL*Loader命令实现数据的输入 

C:\>sqlldr userid=system/manager control=input.ctl 
默认日志文件名为:input.log 
默认坏记录文件为:input.bad 

2.还有一种方法
可以把EXCEL文件另存为CSV(逗号分隔)(*.csv),控制文件就改为用逗号分隔 
LOAD DATA 
INFILE 'd:\car.csv' 
APPEND INTO TABLE t_car_temp 
FIELDS TERMINATED BY "," 
(phoneno,vip_car)

b)在控制文件中直接导入数据

1、控制文件test.ctl的内容
-- The format for executing this file with SQL Loader is:
-- SQLLDR control=<filename> Be sure to substitute your
-- version of SQL LOADER and the filename for this file.
LOAD DATA
INFILE *
BADFILE 'C:\Documents and Settings\Jackey\桌面\WMCOUNTRY.BAD'
DISCARDFILE 'C:\Documents and Settings\Jackey\桌面\WMCOUNTRY.DSC'
INSERT INTO TABLE EMCCOUNTRY
Fields terminated by ";" Optionally enclosed by '"'
(
COUNTRYID NULLIF (COUNTRYID="NULL"),
COUNTRYCODE,
COUNTRYNAME,
CONTINENTID NULLIF (CONTINENTID="NULL"),
MAPID NULLIF (MAPID="NULL"),
CREATETIME DATE "MM/DD/YYYY HH24:MI:SS" NULLIF (CREATETIME="NULL"),
LASTMODIFIEDTIME DATE "MM/DD/YYYY HH24:MI:SS" NULLIF (LASTMODIFIEDTIME="NULL")
)
BEGINDATA
1;"JP";"Japan";1;9;"09/16/2004 16:31:32";NULL
2;"CN";"China";1;10;"09/16/2004 16:31:32";NULL
3;"IN";"India";1;11;"09/16/2004 16:31:32";NULL
4;"AU";"Australia";6;12;"09/16/2004 16:31:32";NULL
5;"CA";"Canada";4;13;"09/16/2004 16:31:32";NULL
6;"US";"United States";4;14;"09/16/2004 16:31:32";NULL
7;"MX";"Mexico";4;15;"09/16/2004 16:31:32";NULL
8;"GB";"United Kingdom";3;16;"09/16/2004 16:31:32";NULL
9;"DE";"Germany";3;17;"09/16/2004 16:31:32";NULL
10;"FR";"France";3;18;"09/16/2004 16:31:32";NULL
11;"IT";"Italy";3;19;"09/16/2004 16:31:32";NULL
12;"ES";"Spain";3;20;"09/16/2004 16:31:32";NULL
13;"FI";"Finland";3;21;"09/16/2004 16:31:32";NULL
14;"SE";"Sweden";3;22;"09/16/2004 16:31:32";NULL
15;"IE";"Ireland";3;23;"09/16/2004 16:31:32";NULL
16;"NL";"Netherlands";3;24;"09/16/2004 16:31:32";NULL
17;"DK";"Denmark";3;25;"09/16/2004 16:31:32";NULL
18;"BR";"Brazil";5;85;"09/30/2004 11:25:43";NULL
19;"KR";"Korea, Republic of";1;88;"09/30/2004 11:25:43";NULL
20;"NZ";"New Zealand";6;89;"09/30/2004 11:25:43";NULL
21;"BE";"Belgium";3;79;"09/30/2004 11:25:43";NULL
22;"AT";"Austria";3;78;"09/30/2004 11:25:43";NULL
23;"NO";"Norway";3;82;"09/30/2004 11:25:43";NULL
24;"LU";"Luxembourg";3;81;"09/30/2004 11:25:43";NULL
25;"PT";"Portugal";3;83;"09/30/2004 11:25:43";NULL
26;"GR";"Greece";3;80;"09/30/2004 11:25:43";NULL
27;"IL";"Israel";1;86;"09/30/2004 11:25:43";NULL
28;"CH";"Switzerland";3;84;"09/30/2004 11:25:43";NULL
29;"A1";"Anonymous Proxy";0;0;"09/30/2004 11:25:43";NULL
30;"A2";"Satellite Provider";0;0;"09/30/2004 11:25:43";NULL
31;"AD";"Andorra";3;0;"09/30/2004 11:25:43";NULL
32;"AE";"United Arab Emirates";1;0;"09/30/2004 11:25:43";NULL
33;"AF";"Afghanistan";1;0;"09/30/2004 11:25:43";NULL
34;"AG";"Antigua and Barbuda";7;0;"09/30/2004 11:25:43";NULL
35;"AI";"Anguilla";7;0;"09/30/2004 11:25:43";NULL
36;"AL";"Albania";3;0;"09/30/2004 11:25:43";NULL
37;"AM";"armenia";3;0;"09/30/2004 11:25:43";NULL
38;"AN";"Netherlands Antilles";3;0;"09/30/2004 11:25:43";NULL
39;"AO";"Angola";2;0;"09/30/2004 11:25:43";NULL
40;"AP";"Asia/Pacific Region";2;0;"09/30/2004 11:25:43";NULL
41;"AQ";"Antarctica";8;0;"09/30/2004 11:25:43";NULL
42;"AR";"Argentina";5;0;"09/30/2004 11:25:43";NULL
43;"AS";"American Samoa";6;0;"09/30/2004 11:25:43";NULL
44;"AW";"Aruba";5;0;"09/30/2004 11:25:43";NULL
45;"AZ";"Azerbaijan";1;0;"09/30/2004 11:25:43";NULL
46;"BA";"Bosnia and Herzegovina";3;0;"09/30/2004 11:25:43";NULL
47;"BB";"Barbados";5;0;"09/30/2004 11:25:43";NULL
48;"BD";"Bangladesh";1;0;"09/30/2004 11:25:43";NULL
49;"BF";"Burkina Faso";2;0;"09/30/2004 11:25:43";NULL
50;"BG";"Bulgaria";3;0;"09/30/2004 11:25:43";NULL
51;"BH";"Bahrain";1;0;"09/30/2004 11:25:43";NULL
52;"BI";"Burundi";2;0;"09/30/2004 11:25:43";NULL
53;"BJ";"Benin";2;0;"09/30/2004 11:25:43";NULL
54;"BM";"Bermuda";4;0;"09/30/2004 11:25:43";NULL
55;"BN";"Brunei Darussalam";1;0;"09/30/2004 11:25:43";NULL
56;"BO";"Bolivia";5;0;"09/30/2004 11:25:43";NULL
57;"BS";"Bahamas";7;0;"09/30/2004 11:25:43";NULL
58;"BT";"Bhutan";1;0;"09/30/2004 11:25:43";NULL
59;"BV";"Bouvet Island";5;0;"09/30/2004 11:25:43";NULL
60;"BW";"Botswana";2;0;"09/30/2004 11:25:43";NULL
61;"BY";"Belarus";3;0;"09/30/2004 11:25:43";NULL
2、执行导入命令
C:\>sqlldr userid=system/manager control=test.ctl


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值