Using an Oracle database link to connect to Excel

5

Recently I had to extract data from 5 Access databases and 4 Excel files and merge the data into an Oracle table. One method was to export the data from Access to Oracle via ODBC (at that stage I had not heard of Workbench ) but I was sure that there must be a way of doing the same but then from Oracle’s side. After searching the web I was able to find bits and pieces and eventually all the pieces fitted together to allow me to connect to Excel and Access by using Oracle Heterogeneous Service, which has been available since Oracle 8i. If one searches the web using “Heterogeneous Service” you will find many links but when I started my search I had never heard of this service. The following Oracle documentation has alot of information on this subject, alot more than I though there was to know, for instance that there are Heterogeneous Service (HS) data dictionary views.
Here are the 5 basic steps required to create a database link to non-Oracle sources:

1. The first step is to create a System DSN in the Windows ODBC Data Source.
Start-up the ODBC Data Source Administrator tool (Control Panel > Administrative Tools), select the System DSN tab and click the add button. I will be using an Excel file in this example but you could do the same for an Access database, SQL Server or even MySQL. Give the Data Source a name e.g DEMO_EXCEL and select the excel file.

2. Next the Hetrogeneous Srvice needs to be configured. Go to the directory <OracleHome>\hs\admin\ and make a copy of the file inithsodbc.ora, rename it e.g. inithsdemo_excel. Ensure that the file name always starts with “iniths”. Open the renamed file and alter the: HS_FDS_CONNECT_INFO to contain the name of the just created ODBC Data Source. For the Excel file I also added “HS_AUTOREGISTER = TRUE” and “HS_DB_NAME = hsodbc” to the file.

HS_FDS_CONNECT_INFO = ” DEMO_EXCEL”
HS_FDS_TRACE_LEVEL = off
HS_AUTOREGISTER = TRUE
HS_DB_NAME = hsodbc

3. Next the Oracle listener needs to be configured. Go to the directory <OracleHome>\Network\Admin\ and open the file listener.ora. Add the following lines to the file (in the same area as the other SID_DESC). The SID_NAME has the name of the file created in the <OracleHome>\hs\admin\ directory (excluding init at the start and .ora at the end).

(SID_DESC =
(SID_NAME = hsdemo_excel)
(ORACLE_HOME = C:\oracle\product\10.1.0\Dbase)
(PROGRAM = hsodbc)
)

For the listener changes to take effect we need to restart the listener ( in dos – lsnrctl stop (start)).

4. All that still needs to be done to be able to create the database link is to add some lines to the tnsnames.ora file in the <OracleHome>\Network\Admin\ directory. Add the following lines to the file where SID contains the same name as was used in SID_NAME in the listener.ora file:

MY_EXCEL.WORLD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SID = hsdemo_excel)
)
(HS = OK)
)

5. Now the Oracle database link can be created and tested.

Create database link my_excel_file using ‘MY_EXCEL.WORLD’

Select *
From “countries$”@ my_excel_file;
–where countries is the name of a worksheet in my excel file.

Some tips:
If you’re querying from an excel file, the worksheets are seen as the tables and you need to add a $ to the end of the names. If you have lower case names in your Access database or Excel file (table or column names)  you need to use lower case in the query and place it between quotes. Finally, if the data in Access or Excel is greater than 255 characters, Oracle will translate it to a long data type and this has caused my sessions to be killed whenever I did a select * type query. Ensure that the Excel file you’re querying is closed, or you will get errors.

That’s how easy it is. Have fun using Oracles HS (you can even use Oracle’s analytic functions in your queries). If you would like to connect to a MySQL database you apparently need to use the MySQL ODBC 3.51 Driver available from MySQL for Windows.

Share.

About Author

5 Comments

  1. I used this several years ago with great success connecting to db2 on an as400. I am now trying to use it on my internal platform to connect from Oracle 9i to MS Access 2007 on the same machine but cannot get it working. Your help would be great! Here is what my config looks like:
    inithsgosales.ora

    HS_FDS_CONNECT_INFO = gosales
    HS_FDS_TRACE_LEVEL = off
    HS_AUTOREGISTER = TRUE
    HS_DB_NAME = hsodbc
    ============================================
    My listener.ora.

    (SID_DESC =
    (SID_NAME = hsgosales)
    (ORACLE_HOME = c:\Oracle\Ora92)
    (PROGRAM = hsodbc)
    )
    =========================================
    my tnsnames.ora

    gosales.mtc =

    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    )
    (CONNECT_DATA =
    (SID = hsgosales)
    )
    (HS=OK)
    )
    ================================================
    TNSPING Results for TNSPING GOSALES.MTC

    Used TNSNAMES adapter to resolve the alias
    Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)
    (KEY = EXTPROC0))) (CONNECT_DATA = (SID = hsgosales)) (HS=OK))
    OK (20 msec)
    =================================================
    My database link syntax:

    drop database link gosales;
    create database link gosales using ‘gosales.mtc’;
    ==================================================
    When I run my select:

    select * from countries@gosales;

    ERROR at line 1:
    ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
    [Generic Connectivity Using ODBC]
    ORA-02063: preceding 2 lines from GOSALES

  2. Good One will Appriciate if you write a step by step guide to create dblink to connect with MS sql 2000 Database

  3. Nice to see someone tackle HS. I’ve never tried Oracle’s ox cart given MS tools (DTS) and Toad’s Data Import features. If the goal is to get current Excel/Access data into Oracle these tools can’t be beat for flexibility, speed, and error reporting.