Monday 9 September 2013

SQL Loader

SQL LOADER utility is used to load data from other data source into Oracle. For example, if you have a table in FOXPRO, ACCESS or SYBASE or any other third party database, you can use SQL Loader to load the data into Oracle Tables. SQL Loader will only read the data from Flat files. So If you want to load the data from Foxpro or any other database, you have to first convert that data into Delimited Format flat file or Fixed length format flat file, and then use SQL loader to load the data into Oracle.
Following is procedure to load the data from Third Party Database into Oracle using SQL Loader.
  1. Convert the Data into Flat file using third party database command.
  2. Create the Table Structure in Oracle Database using appropriate datatypes
  3. Write a Control File, describing how to interpret the flat file and options to load the data.
  4. Execute SQL Loader utility specifying the control file in the command line argument
To understand it better let us see the following case study.
  1. Now come the Linux Machine and create a table in Oracle with the same structure as in MS-ACCESS by taking appropriate datatypes. For example,  create a table like this
$sqlplus scott/tiger
SQL>CREATE TABLE emp (empno number(5),
                name varchar2(50),
                sal  number(10,2),
                jdate date);
  1. After creating the table, you have to write a control file describing the actions which SQL Loader should do. You can use any text editor to write the control file. Now let us write a controlfile for our case study
$vi emp.ctl
1        LOAD DATA
2        INFILE     ‘/u01/oracle/emp.csv’
3        BADFILE         ‘/u01/oracle/emp.bad
4        DISCARDFILE     ‘/u01/oracle/emp.dsc’
5        INSERT INTO TABLE emp
6        FIELDS TERMINATED BY “,” OPTIONALLY ENCLOSED BY ‘”’ TRAILING NULLCOLS
7        (empno,name,sal,jdate date ‘mm/dd/yyyy’)
Notes: (Do not write the line numbers, they are meant for explanation purpose)
1.       The LOAD DATA statement is required at the beginning of the control file.
2.       The INFILE option specifies where the input file is located
3.       Specifying BADFILE is optional. If you specify,  then bad records found during loading will be stored in this file.
4.       Specifying DISCARDFILE is optional. If you specify, then records which do not meet a WHEN condition will be written to this file.
5.       You can use any of the following loading option
1.       INSERT : Loads rows only if the target table is empty
2.       APPEND: Load rows if the target table is empty or not.
3.       REPLACE: First deletes all the rows in the existing table and then, load rows.
4.       TRUNCATE: First truncates the table and then load rows.
6.       This line indicates how the fields are separated in input file. Since in our case the fields are separated by “,” so we have specified “,” as the terminating char for fields. You can replace this by any char which is used to terminate fields. Some of the popularly use terminating characters are semicolon “;”, colon “:”, pipe “|” etc. TRAILING NULLCOLSmeans if the last column is null then treat this as null value, otherwise,  SQL LOADER will treat the record as bad if the last column is null.
7.        In this line specify the columns of the target table. Note how do you specify format for Date columns
  1. After you have wrote the control file save it and then, call SQL Loader utility by typing the following command
$sqlldr userid=scott/tiger control=emp.ctl log=emp.log
After you have executed the above command SQL Loader will shows you the output describing how many rows it has loaded.
The LOG option of sqlldr specifies where the log file of this sql loader session should be created.  The log file contains all actions which SQL loader has performed i.e. how many rows were loaded, how many were rejected and how much time is taken to load the rows and etc. You have to view this file for any errors encountered while running SQL Loader.



Practice

(1) make a control file for sql loader with the name emp.ctl and put the following line code in it

LOAD DATA
INFILE     'c:/emp.csv'
BADFILE         'c:/emp.bad'
DISCARDFILE     'c:/emp.dsc'
INSERT INTO TABLE emp
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS

(empno,name,sal,jdate date 'mm/dd/yyyy')


(2)

 Create table with following script


CREATE TABLE emp 
(
empno number(5),
name varchar2(50),
sal  number(10,2),
jdate date
);              


(3)

Then  place the files emp.ctl and emp.csv  in c drive. suppose you excel sheet data.
emp.ctl is the control file 

emp.csc is the excel sheet which record to load into table


Then run the following command on cmd in window.


sqlldr userid=scott/tiger control='c:/emp.ctl ' log='c:/emp.log'




              






No comments:

Post a Comment