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,
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’;
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));
4)Create the external table: I created the external table as,
SQL> create table external_tab(id char(4),
(default directory exter_dir
(records delimited by newline
fields (id char(4), name char(10)
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;
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.
6)Test the data.
SQL> select * from sample;