SQLLDR 使用小结 (加入修改)

-- The format for executing this file with SQL Loader is:
SQLLDR username/password@DBname control=<filename> log=<logFileName> bad=<badFileName> data=<dataFileName>

1.NULLIF

LOAD DATA
INTO TABLE TABLE111
REPLACE
FIELDS TERMINATED BY x'09'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    COL1              CHAR NULLIF (COL1=BLANKS),
    COL2              DATE "YYYY/MM/DD" NULLIF (COL2=BLANKS),
    COL3              DECIMAL EXTERNAL NULLIF (COL3=BLANKS)
)

注意:COL1 是字段名,不能为YEAR等保留字


Load DATA
INTO TABLE TABLE222
REPLACE
(
COL1    POSITION(01:09) CHAR,
COL2    POSITION(10:19) DECIMAL EXTERNAL
)

DOS执行的时候,使用下面的命令:

cmd.exe /c start sqlldr sqadbuser/sqadbuser@sqa control=C:/SQA/sample.ctl log=C:/SQA/sample.log bad=C:/SQA/sample.bad data=C:/SQA/MASTER/sample.tsv skip=1 rows=5000

在执行的时候,最初没有前面的cmd.exe /c start 的时候

数据有1000多条,只导入了360条程序就那儿了

加上cmd.exe /c start ok了,------这时是另外启动一个dos窗口

另外,commit point的意思也要调查

 

 

2Control File 写法


  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可以在表的开头也可在表的内部字段部分

*****,字段间分隔符,”Record 也就是行间分隔符) 
  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_LINEBEGINDATA后面的数据中是没有直接对应    
  //的列的值的如果第一行改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|    
   

10.使用默认值

control file:

OPTIONS (skip=1, errors=0)

load data

replace

into table tableA

fields terminated by ',' optionally enclosed by '"'

trailing nullcols

(

  product_id ":product_id + 100000000000",

  product "TRIM(:product)",

  --price "TRIM(:price)"

   price  "20000" //使用默认值“20000

)

CSV file:

ProductNo,Product,Price

6011,Leather Cases for Apple iPhone 3G,$127.93

DB data:


 

注:如果没有对应的列,貌似不可以加默认值.如上面的CSV file 不变,但是control file 变为:

OPTIONS (skip=1, errors=0)

load data

replace

into table tableA

fields terminated by ',' optionally enclosed by '"'

trailing nullcols

(

  product_id ":product_id + 100000000000",

  product "TRIM(:product)",

  --price "TRIM(:price)"

   price  "20000" //使用默认值“20000“,

   onemorecol  “aaaaaaa”

)

DB里面有多一列。不可以。
  ==============================================================================    
  象这样的数据     nullif   子句    
   
  10-jan-200002350Flipper   seemed   unusually   hungry   today.    
  10510-jan-200009945Spread   over   three   meals.    
   
  id   position(1:3)   nullif   id=blanks   //  这里可以是blanks   或者别的表达式    
  //   下面是另一个列子   第一行的   1   在数据库中将成为   null    
  LOAD   DATA    
  INFILE   *    
  INTO   TABLE   T    
  REPLACE    
  (n   position(1:2)   integer   external   nullif   n='1',    
  v   position(3    
  )    
  BEGINDATA    
  1   10    
  20lg    
  ------------------------------------------------------------    
   
  如果是英文的日志   格式,可能需要修改环境变量   nls_lang   or   nls_date_format    
   

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值