Loading Data using External Table

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 is as follows,
001 oracle
002 sap
003 Microsoft

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 external_dir as ‘D:\oracle\ext’;
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 external_dir TO HR;
Here current user is HR. 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 sample (id varchar2(10), company varchar2(20));
Table created.

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

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   company
—- ———-
001  oracle
002  sap
003  Microsoft

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 sample;
ID   company
—- ———-
001  oracle
002  sap
003  Microsoft

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s