sqlldr使用小记

       需求:将txt文本格式的数据导入到oracle里面

       解决方案:使用sql*loader工具。

       遇到问题:

       (1) Field in data file exceeds maximum length

       (2)ORA-01861: literal does not match format string

      

        最近需要在不同的数据库之间导入导出数据,我们就戏称是搬运工。就像某矿泉水广告说的一样:我们不生产数据,我们只做数据的搬运工。回归正题,在正式生产运行的时候,使用的IBM datastage工具进行数据转化。但是,也会遇到导出一些数据进行开发使用,这种时候使用DS就有点像重型武器了,不划算。这个时候,脚本就华丽丽的登场了。

       速度使用sqlldr命令,其关键参数 

     userid -- Oracle 的 username/password[@servicename]
    control -- 控制文件,可能包含表的数据
    -------------------------------------------------------------------------------------------------------
    log -- 记录导入时的日志文件,默认为 控制文件(去除扩展名).log
    bad -- 坏数据文件,默认为 控制文件(去除扩展名).bad
    data -- 数据文件,一般在控制文件中指定。用参数控制文件中不指定数据文件更适于自动操作
    errors -- 允许的错误记录数,可以用他来控制一条记录都不能错
    rows -- 多少条记录提交一次,默认为 64
    skip -- 跳过的行数,比如导出的数据文件前面几行是表头或其他描述
   

    使用方法:[oralce@dboracle]$sqlldr user/passwd@orcl control=control.ctl

    其中,控制文件的编写是重点。

    control.ctl内容如下:

    load data                   
    infile 'bank.txt'                              --指定外部数据文件
    append into table test.sqlldr_test   --操作类型
    fields terminated by ","                  --数据中每行记录用","分隔

    Optionally enclosed by '"'               --数据中每个字段用'"'隔起时

    trailing nullcols                              --表的字段没有对应的值时允许为空
    (block_id,
    block_data_begin_date,
    block_data_end_date,
    period_id,
    create_date,
    rec_state,
    state_date)

   注意:

   (1)字段可以指定类型,否则认为是CHARACTER类型。
   (2)表定义

    create table test.SQLLDR_TEST
    (
      BLOCK_ID              INTEGER not null,
      BLOCK_DATA_BEGIN_DATE INTEGER,
      BLOCK_DATA_END_DATE   INTEGER,
      PERIOD_ID             VARCHAR2(8),
      CREATE_DATE           DATE,
      REC_STATE             VARCHAR2(3),
      STATE_DATE            DATE
    )
     此时,使用上述控制文件就会出现ORA-01861: literal does not match format string的错误,解决方案就是把DATE类型的写出来,即

     create_date  DATE   "YYYY-MM-DD"

     state_date   DATE   "YYYY-MM-DD"

      

     在导入数据的时候,也遇到Field in data file exceeds maximum length错误。搬出谷大神之后,有如下之文章:

 

Field exceeds

Symptoms

Loading long columns using sqlldr,the following error is reported:

" Record 1: Rejected - Error on table CRM_ATIC_HEADER_DIF, column LONG_DESC.
Field in data file exceeds maximum length "

Cause

In the control file,char(n) had to be specified for all the "varchar2" and "long" columns that were there in the table definition  

Fix

1.Check if there are any LONG or VARCHAR2 columns in the table where the data has to be loaded

2.Check the control file to make sure whether char(n) has been included for those corresponding columns.

Taking the example of the above control file,if these changes are made:
3.Changing the control file by including char(n) will resolve the issue

    由此可知,只需要把控制文件中的字符类型char(n) 中的n指定数据即可,如:

   period_id           char(8) 
   

    -the end-



       

原文地址:https://www.cnblogs.com/gobird/p/2035468.html