Read XLS or MDB file with OpenDataSource – SQL Server 2005

Read XLS or MDB file with OpenDataSource – SQL Server 2005

Recently, I had a task to transfer data from MDB file to SQL-Server 2005 so I googling little bit and found very good way of doing so. I liked the way and felt it cool so I am sharing with you guys. Before you start this article, Please see my previous article to enable “Ad hoc distributed queries” with sp_configure. Once you enable it. Please create one XLS file, named Emp.XLS, with three columns in “sheet1”.

ID
Name
Dept

Also create one MDB with Employee table and same three fields as above.  After finishing above stuff, we will be moving to heart of this article. We will do three things in this article.

1.)    We will read data from Excel file.

2.)    We will read data from MDB file

3.)    We will create temporary table in SQL-Server 2005 from Excel file’s table.

You can get result set from excel file with following query.

SELECT * FROM OPENDATASOURCE (‘Microsoft.Jet.OLEDB.4.0′, ‘Data Source=\\192.168.1.1\tempexcel\Emp.xls;Extended Properties=Excel 8.0′)…[Sheet1$]

 

You can use local path or UNC path as per your need. Above query will open Emp.XLS file and will get the result set available in Sheet1 of that excel file.

Now we are going to read MDB’s table from within SQL-Server with following query.

SELECT * FROM OPENDATASOURCE (‘Microsoft.Jet.OLEDB.4.0′, ‘Data Source=\\192.168.1.1\tempMDB\Test012709.mdb’)…[Employee]

 

Above query will open MDB file and read its Employee table and return the record set in SQL-Server.

Now at last, we will create one temporary table from Excel file.

SELECT * into #tblTempEmployee FROM OPENDATASOURCE (‘Microsoft.Jet.OLEDB.4.0′, ‘Data Source=\\192.168.1.1\tempexcel\Emp.xls;Extended Properties=Excel 8.0′)…[Sheet1$]

 

This will create temporary #tblTempEmployee table in your current database.

Note: Don’t forget to change path given in above examples.

Reference: Ritesh Shah

6 Comments »

  1. Hemant said

    In Excel File reading it shows error near …[Sheet1$].
    I think thr is wrong syntax. Please tell correct syntax.

    • riteshshah said

      what error are you facing? I just checked this and working absolutely fine.

      SELECT * FROM
      OPENDATASOURCE (’Microsoft.Jet.OLEDB.4.0′, ‘Data Source=C:\email.xls;Extended Properties=Excel 8.0′)…[Sheet1$]

      do remember, you have to have three dots before [sheet1$]

      • Hemant said

        Oh Thanks Ritesh, Its working fine.

        thanks

    • riteshshah said

      please put your further comment at

      http://www.sqlhub.com/2009/03/read-xls-or-mdb-file-with.html

      as SQLHub is my permenent site and I used to support there most of the time.

  2. riteshshah said

    what error are you facing? I just checked this and working absolutely fine.

    SELECT * FROM
    OPENDATASOURCE (‘Microsoft.Jet.OLEDB.4.0′, ‘Data Source=C:\email.xls;Extended Properties=Excel 8.0′)…[Sheet1$]

    do remember, you have to have three dots before [sheet1$]

  3. NR said

    Hi,
    I am getting this:
    “Msg 7403, Level 16, State 1, Line 1
    The OLE DB provider “Microsoft.Jet.OLEDB.4.0″ has not been registered.”
    where do I register it?

    NR

RSS feed for comments on this post · TrackBack URI

Leave a Comment