Saturday, May 20, 2006

Load Excel Sheet data into Oracle Table using SQLLOADER

Question of the Day
Q) Is it possible to load data from an Excel sheet into an Oracle table?A)
First, lets make clear that such a thing is possible. Data can be loaded from an Excel sheet into an oracle table. I will list out one way how to do it, and point out other ways through which the same can be achieved, but those i haven't tried myself yet.

Method 1) We can save the spreadsheet data with a '.CSV' extension. This will save the records in a CSV file with each record in a separate line, each field delimited by a comma. CSV here stands for 'Comma Separated Values'.
This method contains three steps:
1) create a data file.
2) create a control file
3) run SQL* LOADER.
Step 1) creating a data file. Consider we have the following data in the Excel spreadsheet that has been saved in a CSV format. We will name it emp.dat. This will be our data file.
1,vinod,palle
2,pavan,keesara
3,sarath,sura
4,bill,gates
5,larry,ellison
6,larry,wall
Now, this file is specified in the control file. We will see how. But first, lets ensure there is an 'emp' table in the database. Else, we will create it.

$ sqlplus vikram/singh
SQL*Plus: Release 10.1.0.3.0 - Production on Thu May 18 02:37:19 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to:Oracle Database 10g Enterprise Edition Release 10.1.0.3.0 - ProductionWith the Partitioning, OLAP and Data Mining options
select * from tab;
TNAME TABTYPE CLUSTERID

------------------------------ ------- ----------
VIK TABLE

SQL> create table emp(eno number,
ename varchar2(20),
lastname varchar2(20));
Table created.
SQL> desc emp

Name Null? Type
----------------------------------------- -------- ---------------------------- ENO NUMBER
ENAME VARCHAR2(20)
LASTNAME VARCHAR2(20)
SQL> select * from emp;
no rows selected


One can see that there are no records in the emp table. We can insert our data from the .CSV file into the table. We can also append the data into the table using the APPEND keyword.
Step 2) creating a control file.
Our control file may look like the one below. We will call it emp.ctl:

LOAD DATA
INFILE emp.dat
INSERT INTO TABLE emp
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(eno, ename, lastname)


Each line in the control file is as follows:
LOAD DATA: This loads data
INFILE emp.dat: This is the place to specify our datafile. It can also contain '*' which means the data is not in any data file but in the same control file. In the latter case, the data should be specified at the end of the control file.
INSERT INTO TABLE emp: This says to 'sqlldr' to actually insert data into table 'emp'. The other options can be APPEND, and TRUNCATE.FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' This says to the sqlldr utility how to read/parse the data file. In our case, which is CSV, the fields in each line(record) are delimited by a comma. However, there may be any character to delimit the fields. An optional character can also be specified.
(eno, ename, lastname) These are the fields in the 'emp' table.
Step 3) run SQL* Loader. This is the actual invocation of the SQL*LOADER. This can be as follows:

$sqlldr vikram/singh control=emp.ctl log=emp.log
SQL*Loader: Release 10.1.0.3.0 - Production on Thu May 18 04:17:16 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Commit point reached - logical record count 6


Thats it! there are a lot many other options that can be given at the command level.
For more details, refer Oracle Documentation.
Verification:
Now is the time to verify if our data is loaded properly.

$ sqlplus vikram/singh
SQL*Plus: Release 10.1.0.3.0 - Production on Thu May 18 03:10:50 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to:Oracle Database 10g Enterprise Edition Release 10.1.0.3.0 - ProductionWith the Partitioning, OLAP and Data Mining options
select * from emp;
ENO ENAME LASTNAME

---------- -------------------- --------------------
1 vinod palle
2 pavan keesara
3 sarath sura
4 bill gates
5 larry ellison
6 larry wall
6 rows selected.


That verifies it!
Now we will see other methods of doing the same. I shall only list the method of doing it. I dont know the procedure.
Method 2) Use the perl module DBI and DBD:Excel modules. Then, perl will read Excel sheet data for you, write it to a named pipe, which would be an input to the SQL* Loader.
The modules can be obtained from the CPAN site.
DBD:Excel http://search.cpan.org/~kwitknr/DBD-Excel-0.06/Excel.pm
DBI http://search.cpan.org/~timb/DBI-1.50/DBI.pm
Check for the latest releases before downloading any of these modules. They come with good documentation.
Method 3) There is a product available in the market, that does exactly the same what we are looking for, and more. However, it costs. more details can be obtained from: http://www.oraxcel.com/projects/sqlxl
There may be other products but i am not aware of it.

Thus ends our journey!
This is surely not an end. If any of you folks out there know a better way of achieving the same thing, or any of you find flaws with the above explanation, feel free to mail me at "vikramsingh120@gmail.com". Your suggestions, complaints are always welcome.
Thank You.

No comments: