3 e.) SQL Loader

download 3 e.) SQL Loader

of 7

Transcript of 3 e.) SQL Loader

  • 8/8/2019 3 e.) SQL Loader

    1/7

    Oracle SQL*Loader

    SQL*Loader is the primary method for quickly populating Oracle tables with datafrom external files. It has a powerful data parsing engine that puts little limitationon the format of the data in the datafile. SQL*Loader is invoked when you specify

    the sqlldr command or use the Enterprise Manager interface.

    SQL*Loader is an integral feature of Oracle databases and is available in allconfigurations.

    Key Features

    SQL*Loader can be used to do the following:

    Load data across a network. This means that a SQL*Loader client can berun on a different system from the one that is running the SQL*Loader

    server. Load data from multiple datafiles during the same load session Load data into multiple tables during the same load session Specify the character set of the data Selectively load data Load data from disk, tape, or named pipe Specify the character set of the data Generate sophisticated error reports, which greatly aid troubleshooting Load arbitrarily complex object-relational data Use either conventional or direct path loading.

    The SQL*Loader environment

  • 8/8/2019 3 e.) SQL Loader

    2/7

    Log file tells you the state of the tables and indexes and the number oflogical records already read from the input datafile. This information canbe used to resume the load where it left off.

    Bad file or reject file gives you the records that were rejected because offormatting errors or because they caused Oracle errors.

    Discard file specifies the records that do not meet any of the loadingcriteria like when any of the WHEN clauses specified in the control file.These records differ from rejected records.

    Structure of the data file:

    The data file can be in fixed record format or variable record format.

    Fixed Record Format would look like the below. In this case you give a specificposition where the Control file can expect a data field:

    7369 SMITH CLERK 7902 12/17/1980 800

    7499 ALLEN SALESMAN 7698 2/20/1981 1600

    7521 WARD SALESMAN 7698 2/22/1981 1250

    7566 JONES MANAGER 7839 4/2/1981 2975

    7654 MARTIN SALESMAN 7698 9/28/1981 1250

    7698 BLAKE MANAGER 7839 5/1/1981 2850

    7782 CLARK MANAGER 7839 6/9/1981 2450

    7788 SCOTT ANALYST 7566 12/9/1982 3000

    7839 KING PRESIDENT 11/17/1981 5000

    7844 TURNER SALESMAN 7698 9/8/1981 1500

    7876 ADAMS CLERK 7788 1/12/1983 1100

    7900 JAMES CLERK 7698 12/3/1981 950

    7902 FORD ANALYST 7566 12/3/1981 3000

    7934 MILLER CLERK 7782 1/23/1982 1300

    Variable Record Format would like below where the data fields are separated bya delimiter.

    Note: The Delimiter can be anything you like. In this case it is "|"

    1196700|9|0|692.64

    1378901|2|3900|488.621418700|2|2320|467.92

    1418702|14|8740|4056.36

    1499100|1|0|3.68

    1632800|3|0|1866.66

    1632900|1|70|12.64

  • 8/8/2019 3 e.) SQL Loader

    3/7

  • 8/8/2019 3 e.) SQL Loader

    4/7

    (header, feedback, errors, discards, partitions, all)

    DIRECT = {TRUE | FALSE} --Use direct path (Default FALSE)

    PARALLEL = {TRUE | FALSE} -- Perform parallel load (Default FALSE)

    LOAD DATA statement is required at the beginning of the control file.

    INFILE: INFILE keyword is used to specify location of the datafile or datafiles.

    INFILE * specifies that the data is found in the control file and not in an externalfile. INFILE '$FILE', can be used to send the filepath and filename as aparameter when registered as a concurrent program.

    INFILE '/home/vision/kap/import2.csv' specifies the filepath and the filename.

    Example where datafile is an external file:

    LOAD DATA

    INFILE '/home/vision/kap/import2.csv'

    INTO TABLE kap_emp

    FIELDS TERMINATED BY ","

    ( emp_num, emp_name, department_num, department_name )

    Example where datafile is in the Control file:

    LOAD DATA

    INFILE *

    INTO TABLE kap_emp

    FIELDS TERMINATED BY ","

    ( emp_num, emp_name, department_num, department_name )

    BEGINDATA

    7369,SMITH,7902,Accounting

    7499,ALLEN,7698,Sales

    7521,WARD,7698,Accounting

    7566,JONES,7839,Sales

    7654,MARTIN,7698,Accounting

    Example where file name and path is sent as a parameter when registered as aconcurrent program

    LOAD DATAINFILE '$FILE'

    INTO TABLE kap_emp

    FIELDS TERMINATED BY ","

    ( emp_num, emp_name, department_num, department_name )

  • 8/8/2019 3 e.) SQL Loader

    5/7

    TYPE OF LOADING:

    INSERT -- If the table you are loading is empty, INSERT can be used.

    APPEND -- If data already exists in the table, SQL*Loader appends the newrows to it. If data doesn't already exist, the new rows are simply loaded.

    REPLACE -- All rows in the table are deleted and the new data is loadedTRUNCATE -- SQL*Loader uses the SQL TRUNCATE command.

    INTO TABLE is required to identify the table to be loaded into. In the aboveexample INTO TABLE "APPS"."BUDGET", APPS refers to the Schema andBUDGET is the Table name.

    FIELDS TERMINATED BY specifies how the data fields are terminated in thedatafile.(If the file is Comma delimited or Pipe delimited etc)

    OPTIONALLY ENCLOSED BY '"' specifies that data fields may also be enclosedby quotation marks.

    TRAILING NULLCOLS clause tells SQL*Loader to treat any relatively positionedcolumns that are not present in the record as null columns.

    Loading a fixed format data file:

    LOAD DATA

    INFILE 'sample.dat'

    INTO TABLE emp

    ( empno POSITION(01:04) INTEGER EXTERNAL,

    ename POSITION(06:15) CHAR,

    job POSITION(17:25) CHAR,

    mgr POSITION(27:30) INTEGER EXTERNAL,

    sal POSITION(32:39) DECIMAL EXTERNAL,

    comm POSITION(41:48) DECIMAL EXTERNAL,

    deptno POSITION(50:51) INTEGER EXTERNAL)

    Steps to Run the SQL* LOADER from UNIX:

    At the prompt, invoke SQL*Loader as follows:

    sqlldr USERID=scott/tiger CONTROL= LOG=

    SQL*Loader loads the tables, creates the log file, and returns you to the systemprompt. You can check the log file to see the results of running the case study.

    Register as concurrent Program:

    Place the Control file in $CUSTOM_TOP/bin.

    Define the Executable. Give the Execution Method as SQL*LOADER.

    Define the Program. Add the Parameter for FILENAME.

  • 8/8/2019 3 e.) SQL Loader

    6/7

    Skip columns:

    You can skip columns using the 'FILLER' option.

    Load Data------TRAILING NULLCOLS(name Filler,Empno ,sal)

    here the column name will be skipped

    File Types

    SQL*Loader Control FileThe control file is a text file written in a language that SQL*Loader understands.The control file tells SQL*Loader where to find the data, how to parse andinterpret the data, and where to insert the data.

    Input Data and DatafilesSQL*Loader reads data from one or more files specified in the control file. FromSQL*Loader's perspective, the data in the datafile is organized as records. Aparticular datafile can be in fixed record format, variable record format, or streamrecord format. The chosen format depends on the data and depends on the

    flexibility and performance necessary for the job.

    LOBFILEs

    LOB data can be lengthy enough that it makes sense to load it from a LOBFILE.LOB data instances are still considered to be in fields, but these fields are notorganized into records. Therefore, the processing overhead of dealing withrecords is avoided. This type of or organization of data is ideal for LOB loading.

    Bulk Loads

    You can use SQL*Loader to bulk load objects, collections, and LOBs.SQL*Loader supports the following bulk loads:

    Two object types: column objects and row objects Load data from multiple datafiles during the same load session Two collection types: nested tables and VARRAYS Four LOB types: BLOBs, CLOBs, NCLOBs, and BFILEs.

  • 8/8/2019 3 e.) SQL Loader

    7/7

    Load Methods

    SQL*Loader provides three methods to load data: Conventional Path, DirectPath, and External Table.

    Conventional Path LoadConventional path load builds an array of rows to be inserted and uses the SQLINSERT statement to load the data. During conventional path loads, inputrecords are parsed according to the field specifications, and each data field iscopied to its corresponding bind array. When the bind array is full (or no moredata is left to read), an array insert is executed.

    Direct Path LoadA direct path load builds blocks of data in memory and saves these blocksdirectly into the extents allocated for the table being loaded. A direct path loaduses the field specifications to build whole Oracle blocks of data, and write the

    blocks directly to Oracle datafiles, bypassing much of the data processing thatnormally takes place. Direct path load is much faster than conventional load, butentails some restrictions.

    A parallel direct path load allows multiple direct path load sessions toconcurrently load the same data segments. Parallel direct path is more restrictivethan direct path.

    External Table LoadAn external table load creates an external table for data in a datafile andexecutes INSERT statements to insert the data from the datafile into the target

    table.

    There are two advantages of using external table loads over conventional pathand direct path loads:

    An external table load attempts to load datafiles in parallel. If a datafile isbig enough, it will attempt to load that file in parallel.

    An external table load allows modification of the data being loaded byusing SQL functions and PL/SQL functions as part of the INSERTstatement that is used to create the external table.

    SummarySQL*Loader is a high-speed data loading utility that loads data from external filesinto tables in an Oracle database. It provides database administrators with thefast performance and flexibility required to get load jobs conducted as quicklyand efficiently as possible.