18

Having an interesting issue. I'm reading from an excel file on a server via an OpenRowset in Sql2005. I've run the query a number of times without any problems. I've just gone out for a quick meeting and suddenly I get the error "Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)""

I've made sure the files are not in use on the server and even deleted them and recopied them over onto the server and still I'm getting the same error.

UPDATE: This only seems to happen if I join two selects from different openrowsets. If I run the queries individually they still work fine. I have done the join before without any issues. Ideas?

StevenMcD
  • 17,262
  • 11
  • 42
  • 54

7 Answers7

23

The problem comes because the Temp folder of the User under which the SQL server service is running isn't accessible under the credentials which the query is running. Try to to set the security of this temp folder with minimal restrictions. The dsn that gets created every time you run an openrowset query then can be recreated without any credentials conflict. This worked for me without any restart requirements.

Rajesh
  • 246
  • 2
  • 2
  • 3
    Where would I find this Temp folder? – Slider345 Jan 07 '13 at 17:37
  • 4
    For our specific configuration - Windows Server 2008 R2, SQL Server 2008 R2 - the specific TEMP folder we needed to share with domain users was: `C:\Users\\AppData\Local\Temp` – fresh Oct 15 '13 at 20:50
  • 1
    I've spent all day on this - I can't believe this is the solution!!! But it worked so I am happy. – Warren Mar 05 '14 at 00:59
  • Without the permission fresh shared, you will only be able to run the script locally by open SSMS on the SQL Server. Another way is to add your own domain account to the local (SQL Server) Administrators group, which does the same trick. – Chjquest Feb 18 '16 at 15:48
  • Thank you so much, this answer has saved us so much time! – MartynJones87 Mar 17 '16 at 08:34
  • I just found that our SQL Server service is running as "Network Service," and that account has no folder in C:\Users. It does have a temp folder, and you can find it with [these instructions](http://stackoverflow.com/questions/5420898/find-temp-folder-for-user-network-service#5420961). Changing the permissions on this folder for Domain Admins and Domain Users did not eliminate the error for me, however. – rg89 Apr 27 '16 at 17:10
  • This was very useful. I tried everything and at last, this worked for me. I don't know you need access on SQL server temp folder when we are using SQL login. – Shoeb Siddique May 03 '17 at 16:32
  • I know it's redundant and not recommended, but thank you! I had this issue and I'd never guess it was temp table access issues. I just provided full access to AppData and it instantly worked again. It was a Linked Server to Postgres using ODBC. – Hikari Aug 07 '17 at 17:41
6

We ended up restarting the database server and that seemed to solve the problem. Maybe the files were getting locked somehow. We'll never know for sure though

StevenMcD
  • 17,262
  • 11
  • 42
  • 54
  • Server restart worked for me as well. Ended up scripting linked server creation/drop in the stored procedure that performs input. – ajeh Apr 10 '15 at 16:27
  • Restarting server works for me as charm. thank you i can not believe i spend how much time to solve this issue. thank you – Pouya Samie Dec 14 '15 at 14:20
3

I had to download and install "Microsoft Access Database Engine 2010 Redistributable" available here.

"The cause of this issue is that there is no 64-bit ODBC text driver installed on your 64-bit Windows server 2003 actually. The 64-bit MSDASQL just provides an OLEDB/ODBC 'bridge' that allows applications built on OLEDB and ADO (which uses OLEDB internally) to access data sources through ODBC drivers." Source

dialex
  • 2,706
  • 8
  • 44
  • 74
1
/* Linked server between local(Client) SQL server and Remote SQL server 2005*/

USE master
GO
-- To use named parameters: Add linked server in the source (Local machine - eg: MachineName or LocalSeverLoginName)

sp_addlinkedserver 
 @server = N'LnkSrv_RemoteServer_TEST', 
 @srvproduct=N'', -- Leave it blank when its not 'SQL Server'
 @provider=N'SQLNCLI', -- see notes
 @datasrc=N'RemoteServerName', 
 @provstr=N'UID=sa;PWD=sa;'
 --,@catalog = N'MYDATABASE' eg: pubs
GO

/*
 Note: 
  To check provider name use the folling query in the destination server
   Select Provider From sys.servers
*/
----------------------------------------------------------------------------------------------------------
-- Optional
--EXEC sp_addlinkedsrvlogin 'LnkSrv_RemoteServer_TEST', 'true' -- (self is true) -- for LocalSeverLoginName
--GO

-- Remote login
sp_addlinkedsrvlogin
 @rmtsrvname = 'LnkSrv_RemoteServer_TEST',
 @useself = 'False',
 @rmtuser = 'sa',
 @rmtpassword = 'sa'
GO

-- OR
/*
IF the above add linked server login failed then try in the Linked Server (LnkSrv_RemoteServer_TEST) Property 
Select -> Security - > 'For a login not defined in the list above, Connection will:'

Choose - > Be made using this security context
SET Remote login: sa
With password: sa
*/
----------------------------------------------------------------------------------------------------------

-- Test server connection
declare @srvr nvarchar(128), @retval int;
set @srvr = 'LnkSrv_RemoteServer_TEST';
begin try
    exec @retval = sys.sp_testlinkedserver @srvr;
end try
begin catch
    set @retval = sign(@@error);
end catch;
if @retval <> 0
  raiserror('Unable to connect to server. This operation will be tried later!', 16, 2 );

-- OR

BEGIN TRY 
    EXEC sp_testlinkedserver N'LnkSrv_RemoteServer_TEST'; 
END TRY 
BEGIN CATCH 
    PRINT 'Linked Server not available'; 
    RETURN; 
END CATCH 
----------------------------------------------------------------------------------------------------------

-- Get access linked server database
SET xact_abort ON 
GO

BEGIN TRANSACTION
SELECT  *  FROM LnkSrv_RemoteServer_TEST.DBName.dbo.tblName 
COMMIT TRAN
GO

-- OR
SELECT * FROM OPENQUERY(LnkSrv_RemoteServer_TEST, 'SELECT * FROM DBName.dbo.tblName')
GO

-- OR
SELECT * FROM OPENQUERY(LnkSrv_RemoteServer_TEST, 'SELECT * FROM sys.databases Order by name')
GO
----------------------------------------------------------------------------------------------------------
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
0

This issue happened to me, as well. A combination of enabling the "Allow inprocess" provider option for the OraOLEDB.Oracle provide (SSMS > Server Objects > Linked Servers > Provides > OraOLEDB.Oracle), restarting the SQL Server Windows service and lastly adjusting the permissions on the TNSNAMES.ora file directly.

KirstieBallance
  • 1,238
  • 12
  • 26
0

I think you made an ODBC using a 32bit driver but SQL Server Management Studio requires 64 bit. Because this has happened when you want to create a linked server for MYSQL on SQL Server. So, you should make the DSN System use for 64bit drivers

AgungCode.Com
  • 677
  • 6
  • 9
0

Another option is to add an SQL Server login that uses SQL Server authentication and log in using that account. You'll need to go to Security in the server instance properties and select SQL Server and Windows Authentication mode and may need to make sure that the necessary roles are assigned to that user.

Ron
  • 61
  • 3