Thursday, January 8, 2009

How to load data using external table into oracle database

Using SQL*Loader we can load data from flat data file to oracle database which is described in How to load data using sql*loader into oracle database. The external tables feature is a complement to existing SQL*Loader functionality.

In the following section, I will show with an example of how you can load data using external table.

1)Prepare your data file.
This file is where my input data is which will be loaded to database table. Here my datafile data.dat.txt is as follows,
045 KHALEEL
012 SHAIK
022 ABDUL

2)Create the database directory where data file resides.
This data file is not oracle data file rather it is the file where data resides. This oracle database directory is just a operating system path synonym. This directory is the logical path of physical datafile path. As my data file is in the directory c:\oracle\data\ so I create the directory as

SQL> create directory exter_dir as 'E:\oracle\work';

Directory created.

Note that user must have grant create any directory privilege. If other user create the directory then current user must have read access to the directory.
SQL> GRANT READ ON DIRECTORY exter_dir TO SHIAK;
Here current user is sHAIK. So execute above statement with a user that have privilege.

3)Prepare the original Table: You should ignore this step if your desired table already exist in your database. If not exist then create a new one. I created as,

SQL> create table std_name (id varchar2(10), name varchar2(20));
Table created.

4)Create the external table: I created the external table as,
SQL> create table external_tab(id char(4),
name char(10))
organization external
(default directory exter_dir
ACCESS parameters
(records delimited by newline
fields (id char(4), name char(10)
)
)
location ('data.dat.txt')
);


Table created.

Here "organizational external" indicates this one is external table.
"default directory" indicates the name of the directory where data file resides.
"location" indicates the name of the data file.

In this phase data from the data file is loaded into the external table. You can check whether it is successfully load or not by issuing,
SQL> select * from external_tab;
ID NAME
---- ----------
045 KHALEEL
012 SHAIK
022 ABDUL

5) Load the data from the external table to database table:
Load data from external_tab into the table std_name by,

SQL> insert into std_name select * from external_tab;
3 rows created.

SQL> commit;
Commit complete
.

6)Test the data.
SQL> select * from std_name;

ID NAME
---------- --------------------
045 KHALEEL
012 SHAIK
022 ABDUL

No comments:

Post a Comment