oracle sql and dba tutorial logo

Oracle DBA

Learn Oracle 11g / 12c Database Admin step by step



Oracle SQL

Tutorial for Oracle SQL

 

Oracle DBA Interview Questions

Most asked Oracle DBA Interview Questions.

60 Technical Questions
42 Backup & Recovery Questions

Unix For Oracle DBA 20 Questions


Download Oracle 11g / 12 c Software

Links to Download Oracle 12c / 11g for Linux, Windows etc.



Loading data from Fixed Width files into Oracle Database

CASE STUDY (Loading Data from Fixed Length file into Oracle)

Suppose we have a fixed length format file containing employees data, as shown below, and wants to load this data into an Oracle table.

7782	CLARK	MANAGER		7839  2572.50          10 

7839 KING PRESIDENT 5500.00          10
7934 MILLER CLERK 7782 920.00          10
7566 JONES   MANAGER 7839 3123.75          20
7499 ALLEN SALESMAN 7698 1600.00   300.00 30
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30
7658 CHAN ANALYST 7566 3450.00 20
7654 MARTIN SALESMAN 7698  1312.50 1400.00 30

 

SOLUTION:

Steps :-

1.      First Open the file in a text editor and count the length of fields, for example in our fixed length file, employee number is from 1st position to 4th position, employee name is from 6th position to 15th position, Job name is from 17th position to 25th position. Similarly other columns are also located.

2.      Create a table in Oracle, by any name, but should  match columns specified in fixed length file. In our case give the following command to create the table.

SQL> CREATE TABLE emp (empno  NUMBER(5),
        name VARCHAR2(20),
        job  VARCHAR2(10),
        mgr  NUMBER(5),
        sal  NUMBER(10,2),
        comm NUMBER(10,2),
        deptno NUMBER(3) );

3.      After creating the table, now write a control file by using any text editor

$ vi empfix.ctl
1)   LOAD DATA
2)   INFILE '/u01/oracle/fix.dat'
3)   INTO TABLE emp
4)   (empno POSITION(01:04)   INTEGER EXTERNAL,
       name 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,
5)   deptno POSITION(50:51)   INTEGER EXTERNAL)

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 name of the file containing data follows the INFILE parameter.
3.       The INTO TABLE statement is required to identify the table to be loaded into.
4.       Lines 4 and 5 identify a column name and the location of the data in the datafile to be loaded into that column. empno, name, job, and so on are names of columns in table emp. The datatypes (INTEGER EXTERNAL, CHAR, DECIMAL EXTERNAL) identify the datatype of data fields in the file, not of corresponding columns in the emp table.

5.       Note that the set of column specifications is enclosed in parentheses.

 

4.      After saving the control file now start SQL Loader utility by typing the following command.

$sqlldr userid=scott/tiger control=empfix.ctl log=empfix.log direct=y

After you have executed the above command SQL Loader will shows you the output describing how many rows it has loaded.

Loading Data into Multiple Tables using WHEN condition

You can simultaneously load data into multiple tables in the same session. You can also use WHEN condition to load only specified rows which meets a particular condition (only equal to “=” and not equal to “<>” conditions are allowed).

For example, suppose we have a fixed length file as shown below

7782	CLARK	MANAGER		7839  2572.50          10 

7839 KING PRESIDENT 5500.00          10
7934 MILLER CLERK 7782 920.00          10
7566 JONES   MANAGER 7839 3123.75          20
7499 ALLEN SALESMAN 7698 1600.00   300.00 30
7654 MARTIN SALESMAN 7698 1312.50 1400.00 30
7658 CHAN ANALYST 7566 3450.00 20
7654 MARTIN SALESMAN 7698  1312.50 1400.00 30

 

Now we want to load all the employees whose deptno is 10 into emp1 table and those employees whose deptno is not equal to 10 in emp2 table. To do this first create the tables emp1 and emp2 by taking appropriate columns and datatypes. Then, write a control file as shown below

$vi emp_multi.ctl

Load Data
infile ‘/u01/oracle/empfix.dat’
append into table scott.emp1
WHEN (deptno=’10 ‘)
  (empno POSITION(01:04)   INTEGER EXTERNAL,
   name 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)
  INTO TABLE scott.emp2
  WHEN (deptno<>’10 ‘)
  (empno POSITION(01:04)   INTEGER EXTERNAL,
   name 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)

After saving the file emp_multi.ctl run sqlldr

$sqlldr userid=scott/tiger control=emp_multi.ctl 

 

 

 


HomeContact Us

Data Loader

Data Loader is a simple yet powerful tool to
export and import Data between many common database formats


Forms Data Loader

Tool to load data into Oracle E-Business Suite R12 / Oracle Apps using Macros and Forms Record and Playback

Interface Computers Academy © 2007-2017 All Rights Reserved