Querying an Oracle database from SQL Server - sql-server

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

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

select *
from [server]..[xxx].[yyyyy]
It works for me.

Change
,#provider = 'MSDASQL'
with
,#provider = 'MSDAORA'

Related

Dynamic SQL Error -104 from Linked Firebird Database

I have a SQL Server database and a Firebird database as a linked server. I'm trying to select a table in the linked server, but I get the following error:
OLE DB provider "MSDASQL" for linked server "FIREBIRD" returned message "[ODBC Firebird Driver][Firebird]Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 22
.".
Msg 7306, Level 16, State 2, Line 4
Cannot open the table ""de_ib"."ROHSTOFF"" from OLE DB provider "MSDASQL" for linked server "FIREBIRD".
I have synonyms for the linked tables set up in my SQL database. The following three statements all return this error:
SELECT * FROM ROHSTOFF
SELECT * FROM [dbo].[ROHSTOFF]
SELECT * FROM [FIREBIRD].[de_ib]..[ROHSTOFF]
But the following returns the data as expected:
SELECT * FROM OPENQUERY(FIREBIRD, 'SELECT * FROM ROHSTOFF')
If I right-click a linked table and select Script Table as > INSERT To > New Query Editor Window, the following appears in the query editor:
-- [FIREBIRD].[de_ib]..[ROHSTOFF] contains no columns that can be selected or the current user does not have permissions on that object.
I'm logged in with credentials that match the owner of the Firebird database.
Why am I getting this error and how can I select data from my linked server without using OPENQUERY? I have a legacy application that selects from the Firebird database, but I need it to select seamlessly from the synonyms instead.

sp_rda_reauthorize_db not creating remote database copy

I have a db which is stretch enabled in SQL server 2016 RC3.
I took local backup of that database, and restored successfully. As we know that after restore the link to Azure SQL Server will get break, therefore I am using the below mention T-SQL to resume the connectivity, additionally I am specifying #with_copy = 1, so that it create a copy of database in Azure SQL server. But below mention T-SQL always fails with error as
Copying remote database 'RDADB_A40A50B5B-386A-42C4-B19C-3C2516172CAF' to remote database 'RDADB_A_Clone74FF1F31-A689-4866-BDD1-F90D15C157BB'.
OLE DB provider "SQLNCLI11" for linked server "stretchserver-sc-server-dbstrech-20160314-114758814.database.windows.net" returned message "Query timeout expired".
Msg 7399, Level 16, State 1, Procedure sp_rda_reauthorize_db, Line 1 [Batch Start Line 5]
The OLE DB provider "SQLNCLI11" for linked server "stretchserver-sc-server-dbstrech-20160314-114758814.database.windows.net" reported an error. Execution terminated by the provider because a resource limit was reached.
Msg 7320, Level 16, State 2, Procedure sp_rda_reauthorize_db, Line 1 [Batch Start Line 5]
Cannot execute the query "CREATE DATABASE [RDADB_A_Clone74FF1F31-A689-4866-BDD1-F90D15C157BB] AS COPY OF [RDADB_A40A50B5B-386A-42C4-B19C-3C2516172CAF]" against OLE DB provider "SQLNCLI11" for linked server "stretchserver-sc-server-dbstrech-20160314-114758814.database.windows.net".
CREATE DATABASE SCOPED CREDENTIAL AzureCred WITH IDENTITY = 'AzureAdministrator',
SECRET = '****' Declare #credentialName nvarchar(128);
SET #credentialName = N'AzureCred';
EXEC sp_rda_reauthorize_db #credential = #credentialName, #with_copy = 1
Note :- I have configured the firewall of Azure SQL server to include my IP.
The create copy part is a regression. I believe the fix is still being worked on. Please file a bug on https://connect.microsoft.com/SQLServer/feedback/ if you want to track its progress. In the meantime, you can set the option to 0 or false to avoid creating a copy. If you do need to make copies, let us know what your scenario is and we'll see if we can find you temporary workarounds.

SQL Server 2008 - Linked Firebird Server - not working properly

I have a SQL Server 2008. I have created an ODBC connection to a Firebird database using the Firebird/InterBase(r) driver.
The connection works ok.
Then I created a linked server in SQL Server 2008 using the Microsoft OLE DB Provider for ODBC diver.
Everything seems to work fine. I can pull data from tables using
SELECT * FROM OPENQUERY(LINKEDSERVERNAME, 'SELECT * FROM TABLENAME')
with success.
My problem is remote store procedure call. On the Firebird side there is a stored procedure that has one input parameter. Sometimes I get the result, but most of the time I get an error. I am not sure why it is happening, but I don't understand much from the error.
I call the procedure like this:
SELECT * FROM OPENQUERY(LINKEDSERVERNAME, 'SELECT * FROM GET_DOCUMENT_S(05011002766916)'
Its either I get the result (1 row) or I get an error. For id 05011002766916i always get the result, but for some id I never get an result, even tho the Firebird admin claims that the same procedure call returns result normally on his client.
Here are the errors I am getting:
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "MSDASQL" for linked server "LINKEDSERVERNAME" reported an error. The provider did not give any information about the error.
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT * FROM GET_DOCUMENT_S(04170200133A120)" for execution against OLE DB provider
"MSDASQL" for linked server "LINKEDSERVERNAME".
Or:
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "MSDASQL" for linked server "LINKEDSERVERNAME" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "MSDASQL" for linked server "LINKEDSERVERNAME".
Can someone please explain what could be the issue, as currently I dont know if its a problem in ODBC, linked server or Firebird side...
Thank you.
The problem was on Firebird side. I used Flamerobin client and it produced IBPP:SQLException

How to query remote index catalogs

I've been trying to create a linked server in SQL Server that accesses a
remote Index Service catalog, but I can't seem to do it. Let's call
the remote server "remoteServer" and the Catalog "remoteCatalog"
I've tried this:
EXEC sp_addlinkedserver remoteIndexServer, 'Index Server', 'MSIDXS',
'query://remoteServer/remoteCatalog'
and then i did run the SQL
SELECT * FROM OPENQUERY(remoteIndexServer,'select filename from scope()') AS Q
But i got the error as
OLE DB provider "MSIDXS" for linked server "remoteIndexServer" returned message "Service is not running. ".
Msg 7320, Level 16, State 2, Line 3
Cannot execute the query "select filename from scope()" against OLE DB provider "MSIDXS" for linked server "remoteIndexServer".
I have experienced this issue before. This is from memory so excuse any errors but if I recall correctly you will need to do the following.
Install the indexing service on your local SQL Server (this is so the provider is available).
Add a linked server to this LOCAL indexing service.
You can then run you query as below
SELECT *
FROM OPENQUERY(
LocalLinkedServer,
'select filename from RemoteServer.CatalogName..scope()'
) AS Q
If that doesn't work let me know, there is a post somewhere that describes how to do this. I can look it up if necessary but I think the above is right.

Force addition/deletion of a linked server and correct syntax to import data into a table from Excel

I'm trying to create a linked server in Sql server 2008 R2, just tried downloading the '64-bit version of the Office 2010 Access AD Engine' exe from Microsoft.
Unfortunately, I had tried adding a linked server, BEFORE making this installation, and it failed:
EXEC sp_addlinkedserver 'LinkedServer1', 'Excel', 'Microsoft.Jet.OLEDB.4.0', 'D:\Folder\Excel1.xls', '', 'Excel 8.0', ''
(let me know if this way of adding is not correct if you have another way to do it !)
After making the download, it gave a message that this server already exists :
"There are still remote logins or linked logins for the server 'LinkedServer1'."
Tried adding 'LinkedServer2' in the same way AFTER the download (used Excel5.0 as the parameter instead of Excel8.0). It works!
However, this old linked server seems to still exist, though I've tried dropping it:
To be sure it's actually there, I wrote this:
select * from sys.servers where is_linked =1
and the properties in detail:
server_id: 1
name= LinkedServer1
product= Excel
provider = Microsoft.Jet.OLEDB.4.0
data_source= D:\Folder\Excel1.xls
location:NULL
provider_string: Excel 8.0
is_linked=1
is_data_access_enabled=1
modify_date= 2010-08-15 19:56:02.307
Let me know if you notice me doing something wrong! I tried dropping 'LinkedServer2', it works! However, even when I'm trying to drop the same linked server1 like this:
Exec sp_dropserver #server = 'LinkedServer1'
fails on me, same message!
"There are still remote logins or linked logins for the server 'LinkedServer1'."
Tried adding a linked server using provider string as 'Excel 5.0' and it was added and dropped successfully.
Another Update:
This is failing again:
EXEC(' INSERT INTO Table1
SELECT col1, excel2col, excel3col, Replace(Replace(excel4col, ''"'', '''')
FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''Excel 5.0;HDR=Yes;Database='+'D:\Folder\Excel1.xls'', [Sheet2$])')
Error:
OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.
I have changed configuration to :
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
Looks like a 64-bit/Sql Server R2 issue:
to delete a linked server explicitly , we should use:
sp_dropserver 'LinkedServer1', 'droplogins';
This solves the problem of an invalid linked server.
however, the Microsoft.jet.OLEDB.4 is not working, the same error keeps coming up:
<Errors> <Error>ERROR CAUGHT: OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.</Error> </Errors>
Tried downloading the Microsoft.ACE.OLEDB.12.0 and used this to add the linked server.
However, when I try to do an insert into table using OpenRowSet, a new error comes up:
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error. Access denied.
Msg 7301, Level 16, State 2, Line 1
Cannot obtain the required interface ("IID_IDBCreateCommand") from OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
Anyone faced this before, or knows how to solve this?

Resources