Index   Search   Add FAQ   Ask Question  

Oracle SQL*Loader FAQ

$Date: 17-Dec-1999 $
$Revision: 1.22 $
$Author: Frank Naudé $

Topics

  • What is SQL*Loader and what is it good for?
  • How does one use SQL*Loader?
  • Can one load variable and fix length data records?
  • Can one modify data as it loads into the database?
  • Can one load data into multiple tables at once?
  • Can one selectively load only the data that you need?
  • How does one load multi-line records?
  • How can one get SQL*Loader to commit only at the end of the load file?
  • Can one improve the performance of SQL*Loader?
  • What utilities does Oracle supply to download data to a flat file?

  • Back to Oracle FAQ Index

    What is SQL*Loader and what is it good for?

    SQL*Loader is a utility used for moving data from external files into the Oracle database. Its syntax is similar to that of the DB2 Load utility, but comes with more options. SQL*loader supports various load formats, selective filters, and multi-table loads.

  • Back to top of file

  • How does one use SQL*Loader?

    One load data into the Oracle database by using the sqlldr (sqlload on some platforms) utility. Look at the following example:
            sqlldr orauser/passwd control=loader.ctl
    
    This is the control file, loader.ctl:
            load data
              infile *
              replace
              into table departments
              (  dept     position (02:05) char(4),
                 deptname position (08:27) char(20)
              )
            begindata
              COSC  COMPUTER SCIENCE
              ENGL  ENGLISH LITERATURE
              MATH  MATHEMATICS
              POLY  POLITICAL SCIENCE
    
  • Back to top of file

  • Can one load variable and fix length data records?

    Yes, look at the following control file examples. In the first we will load delimited data (variable length):
       LOAD DATA
       INFILE *
       INTO TABLE load_delimited_data
       FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'
       TRAILING NULLCOLS
       (  data1,
          data2
       )
       BEGINDATA
       11111,AAAAAAAAAA
       22222,"A,B,C,D,"
    
    If you need to load positional data (fixed length), look at the following control file example:
       LOAD DATA
       INFILE *
       INTO TABLE load_positional_data
       (  data1 POSITION(1:5),
          data2 POSITION(6:15)
       )
       BEGINDATA
       11111AAAAAAAAAA
       22222BBBBBBBBBB
    
  • Back to top of file

  • Can one modify data as it loads into the database?

    Yes, look at the following examples:
       LOAD DATA
       INFILE *
       INTO TABLE modified_data
       (  rec_no                      "my_db_sequence.nextval",
          region                      CONSTANT '31',
          time_loaded                 "to_char(SYSDATE, 'HH24:MI')",
          data1        POSITION(1:5)  ":data1/100",
          data2        POSITION(6:15) "upper(:data2)",
          data3        POSITION(16:22)"to_date(:data3, 'YYMMDD')"
       )
       BEGINDATA
       11111AAAAAAAAAA991201
       22222BBBBBBBBBB990112
    

       LOAD DATA
       INFILE 'mail_orders.txt'
       BADFILE 'bad_orders.txt'
       APPEND
       INTO TABLE mailing_list
       FIELDS TERMINATED BY ","
       (  addr,
          city,
          state,
          zipcode,
          mailing_addr   "decode(:mailing_addr, null, :addr, :mailing_addr)",
          mailing_city   "decode(:mailing_city, null, :city, :mailing_city)",
          mailing_state
       )
    
  • Back to top of file

  • Can one load data into multiple tables at once?

    Look at the following control file:
       LOAD DATA
       INFILE *
       REPLACE
       INTO TABLE emp
            WHEN empno != ' '
       ( empno  POSITION(1:4)   INTEGER EXTERNAL,
         ename  POSITION(6:15)  CHAR,
         deptno POSITION(17:18) CHAR,
         mgr    POSITION(20:23) INTEGER EXTERNAL
       )
       INTO TABLE proj
            WHEN projno != ' '
       (  projno POSITION(25:27) INTEGER EXTERNAL,
          empno  POSITION(1:4)   INTEGER EXTERNAL
       )
    
  • Back to top of file

  • Can one selectively load only the data that you need?

    Look at this example, (01) is the first character, (30:37) are characters 30 to 37:
       LOAD DATA
       APPEND
       INTO TABLE my_selective_table
       WHEN (01) <> 'H' and (01) <> 'T' and (30:37) = '19991217'
       (
          region              CONSTANT '31',
          service_key         POSITION(01:11)   INTEGER EXTERNAL,
          call_b_no           POSITION(12:29)   CHAR
       )
    
  • Back to top of file

  • How does one load multi-line records?

    One can create one logical record from multiple physical records using one of the following two clauses:

  • Back to top of file

  • How can get SQL*Loader to commit only at the end of the load file?

    You can not, but by setting the ROWS= parameter to a large value, committing can be reduced. Make sure you have big rollback segments ready when you use a high value for ROWS=.

  • Back to top of file

  • Can one improve the performance of SQL*Loader?

    1. A very simple but easily overlooked hint, do not have any indexes and/or constraints (primary key) on your load tables during the load process. This will significantly slowdown load times even with ROWS= set to a high value.

    2. Add the following option in the command line: DIRECT=TRUE. This will effectively bypass most of the RDBMS processing. However, there are cases when you can't use direct load. Refer to chapter 8 on Oracle server Utilities manual.

    3. Turn off database logging by specifying the UNRECOVERABLE option. This option can only be used with direct data loads.

    4. Run multiple load jobs concurrently.

  • Back to top of file

  • What utilities does Oracle supply to download data to a flat file?

    Oracle doesn't supply any data unload tools. However, you can use SQL*Plus to select and format your data and then spool it to a file:
            set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on
            spool oradata.txt
            select col1 || ',' || col2 || ',' || col3
            from   tab1
            where  col2 = 'XYZ';
            spool off
    
    Alternatively use the UTL_FILE PL/SQL package:
            rem Remember to update initSID.ora, utl_file_dir='c:\oradata' parameter
            declare
               fp utl_file.file_type;
            begin
               fp := utl_file.fopen('c:\oradata','tab1.txt','w');
               utl_file.putf(fp, '%s, %s\n', 'TextField', 55);
               utl_file.fclose(fp);
            end;
            /
    

    You might also want to investigate third party tools like Platinum FastUnload or TOAD.

  • Back to top of file

  • General: Home | Index | Preamble | Glossary | OraCorp | Papers | Fun | News | Events | Y2000 | Books | Links | Forums
    Products: SQL | Plus | Loader | PL/SQL | PreComp | OPO | OMO | OO4OLE | DBA | PQO | PSO | OCO | Net | ODBC | WebServer | Des2k | Dev2k
    Systems: MVS | Unix | Windows | WindowsNT | NetWare | VMS