Querying an Oracle database from SQL Server

mikimr picture mikimr · Jul 3, 2012 · Viewed 50.9k times · Source

I have an Oracle 11g XE database that I would like to transfer into SQL Server Express 2005.

At first I thought I'd just generate the tables in Oracle as SQL, manipulate the data formats, and run the query in SQL Server. This worked for small tables, but I have several tables with a few hundred thousands rows and some with millions of rows, so this solution won't work.

I then created a TNS file with the following content:

OracleTnsName = 
(
  DESCRIPTION=
  (
    ADDRESS = (PROTOCOL=TCP)(HOST=localhost)(PORT=1521)
  )
  (
    CONNECT_DATA = (SERVICE_NAME=XE)
  )
)

I followed instructions I found elsewhere on how to generate the ODBC connection, and the 'test connection' was successful.

I then ran these commands to create a Linked Server in MS SQL:

EXEC sp_addlinkedserver 
     @server            = 'OracleLinkServer'
    ,@srvproduct        = 'OracleTnsName'
    ,@provider          = 'MSDASQL'
    ,@datasrc           = 'OracleTnsName'

EXEC sp_addlinkedsrvlogin 
     @rmtsrvname        = 'OracleLinkServer'
    ,@useself           = 'False'
    ,@locallogin        = NULL
    ,@rmtuser           = 'user'
    ,@rmtpassword       = 'password'

Now I'm trying to query a table in the Oracle database from SQL Server using openquery:

select * from openquery(OracleLinkServer, 'select * from oracleTable')

But get an error:

Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "MSDASQL" for linked server "OracleLinkServer" reported an error. The provider did not give any information about the error.
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "OracleLinkServer".

When I check the properties of the Linked Server, and just click the OK, I get this error:

TITLE: Microsoft SQL Server Management Studio Express

"The linked server has been updated but failed a connection test. Do you want to edit the linked server properties?"


ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.Express.ConnectionInfo)


The OLE DB provider "MSDASQL" for linked server "OracleLinkServer" reported an error. The provider did not give any information about the error. Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "OracleLinkServer". (Microsoft SQL Server, Error: 7399)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=09.00.5000&EvtSrc=MSSQLServer&EvtID=7399&LinkId=20476


BUTTONS:

&Yes

&No

Please help!

Thanks

Answer

David M picture David M · Jul 3, 2012

If you have successfully added your linked server, you no longer need OPENQUERY. You can just include the linked server name as the first part of the qualified name like so:

SELECT * FROM OracleLinkServer.database.schema.table

Not sure which parts you need, but the dots are key. Try this first:

SELECT * FROM OracleLinkServer...oracleTable