Home > front end >  SQL Server: accessing CSV files as linked server with Windows authentication
SQL Server: accessing CSV files as linked server with Windows authentication

Time:01-05

Wish you all a happy new year!

I periodically import data from CSV files downloaded from a web application. I have created the linked server LS_Text for the local path with the CSV files. I import the file content with INSERT INTO ... SELECT queries in a stored procedure. This works fine as long as I log in to SSMS as SA. But if I log in with my Windows authentication, I get the error

Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "LS_Text".

I plan to import the files by an application that calls the stored procedure. So Windows authentication has to work.

What means the error message? The same error message results if the file path does not exist. So it looks as if SQL Server or the OLEDB provider can't see the folder with the CSV files. But I have saved the files myself with my credencials.

I have created the linked server with the following batch:

EXEC sp_addlinkedserver @server = N'LS_Text', @srvproduct=N'CSVFLATFILE',
  @provider=N'Microsoft.ACE.OLEDB.12.0', @datasrc=N'C:\Trans', @provstr=N'Text;HDR=Yes';
EXEC sp_addlinkedsrvlogin @rmtsrvname = N'LS_Text', @useself = 'false',
  @locallogin = NULL, @rmtuser = NULL, @rmtpassword = NULL;
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1;

As far as I understand, @useself = 'false', @rmtuser = NULL, @rmtpassword = NULL means that the linked server can be accessed without login and password. I have tried all sort of other combinations, yet without success.

Articles found on Google for this error message deal with OPENROWSET rather than linked server. Or with ACE driver configuration. But this is not the issue since it works as SA.

So how can I query the CSV files with Windows authentication? Any hint is appreciated.

CodePudding user response:

Perhaps this is not a full answer, but it should hopefully help you debug this kind of issue better yourself. Since, as you mention, it works as sa, the likely problem is related to the user/login mapping. There's an example on the doc page sp_addlinedsrvlogin doc describing how to try a specific Windows login. That might be worth trying for your credentials to see if that works. Second, there are ways to delve into what is happening in the server's code path to load and use the provider. A reasonable blog post can be found here which is about talking to Oracle but the important content is about how to set up trace events for linked servers and see what is happening once you start trying to execute your query. (linked server vs. openrowset to a linked server should not matter, but please note that the term openrowset was overloaded in SQL to allow different code paths including some that don't directly go through OLE/DB or not through this specific OLE/DB provider, as David mentions in the comments to your question). So, tracing the actions before the error may point out a spot where things have failed differently in your windows login case vs. the sa/admin path. Finally, as the Jet (now ACE) provider is fundamentally a DLL that gets loaded into the SQL Server process and then does file system operations to try to load a file, it may be valuable to just use procmon to monitor the process and see if there is some operation that is failing (such as reading a registry key or opening a file inside the provider). It doesn't seem to be the most likely problem given that sa works for you, but it may be a useful tool.

You also asked about the error message. I'll try to explain. (I wrote the original Jet OLE/DB provider that was later renamed to ACE after I changed teams). In OLE/DB, there are COM interfaces that conceptually "live" on 4 main internal classes. You can see this documented here in the OLE/DB programmer's guide. The Data Source object is the top-most object and it means somewhat different things to different data sources. The second-level concept is a "session". In Jet/ACE, these two concepts are not really different as you just have a connection to a file, but in SQL Server and other server things, the data source object is a connection to a server and the session is an individual connection to that server. The error you are getting is saying that the initial connection/authentication to the provider is failing. It means one of N things, but I'd start with examining "the mapping from SQL's login to the login for Jet/ACE is not working properly".

net-net - if you can load CSVs through the normal paths (openrowset(BULK CSV format), your life is probably going to be better in the long-run as David suggests.

Best of luck debugging your problem whatever path you pick.

  •  Tags:  
  • Related