OPENROWSET with Excel file - sql-server

I want to execute simple statement:
SELECT * FROM
OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Text;Database=C:\Temp\;','SELECT * FROM [test.csv]')
And suddenly I get this message today morning:
Msg 7308, Level 16, State 1, Line 1
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.
It was working till today morning!
Here is my server specs:
Windows 2008 R2 64 bit
SQL Server 2008 64 bit
I've installed AccessDatabaseEngine_x64.exe.
Sql Server is running under LocalService account.
I've set Everyone to have FullControl permission to "C:\Temp" as well as "C:\Windows\ServiceProfiles\LocalService\AppData\Local".
Is there anything I missed? I'm really confused...
Edited:
I've also executed these statements:
sp_configure ‘show advanced options’, 1;
GO
RECONFIGURE;
GO
sp_configure ‘Ad Hoc Distributed Queries’, 1;
GO
RECONFIGURE;
GO
EXEC master.dbo.sp_MSset_oledb_prop N’Microsoft.ACE.OLEDB.12.0′, N’AllowInProcess’, 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N’Microsoft.ACE.OLEDB.12.0′, N’DynamicParameters’, 1
GO
I've also tested ACE.OLEDB.12.0 with administrator account:
SELECT * FROM
OPENROWSET('MICROSOFT.ACE.OLEDB.12.0','Text;Database=C:\Temp\;','SELECT * FROM [test.csv]')
There is another error:
OLE DB provider "MICROSOFT.ACE.OLEDB.12.0" for linked server "(null)" returned message "Unspecified error".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "MICROSOFT.ACE.OLEDB.12.0" for linked server "(null)".
Finally I found it:
I ran ProcMon and I saw that Sql Server wants to access to F:\Windows Temp\ and the folder does not exist! I created the folder and the issue is solved. But, I never had such a folder!!!

Finally I found it: I ran ProcMon and I saw that SQL Server wants to access to F:\Windows Temp\ and the folder does not exist! I created the folder and the issue is solved. But, I never had such a folder!!!

Related

SQL Server and Microsoft.ACE.OLEDB.12.0

I am getting the following:
Msg 7399, Level 16, State 1, Line 1 The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" 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 "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
When I execute the following from a Stored Procedure in SQLServer -
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=c:\Users\v-bimoss\Source\Workspaces\External\Onesite\URLoadDB\Templates\T1-Current.xlsx', 'SELECT * FROM [Sheet1$]')
I have been through all the threads I could find on this topic - but still am having the problem
I have verified that:
The 64 bit Microsoft.ACE.OLEDB.12.0 drivers are loaded
Ad Hoc Distributed Queries is set to 1
Dynamic Parameter and Allow Inprocess are both set to 1
Changed MSSQL to run under my account
The account that is running MSSQL and the account I am using both have full access to C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp
Any help would be appreciated. -wmm
I had the same problem. After going through a number of answers installing the provider, dealing with the cryptic errors, I had a brief moment of joy when I didn't get an immediate error- instead the query never returned...
I finally got it working after wasting more than a day on it. The final fix was to change SQL Server to the Local System account to login.
Here's what I did:
Ensure Office and SQL Server have the same bit width (64-bit in my case).
Install ACE provider for 64-bit (Jet for 32-bit)
Configure server
USE [MSDB]
GO
sp_configure 'show advanced options', 1
GO
RECONFIGURE WITH OverRide
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE WITH OverRide
GO
USE [master]
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'AllowInProcess', 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'DynamicParameters', 1
GO
Configure SQL logon. In SQL Server Configuration Manager
Open the server Properties -> Log On -> Log on as:
Change to Built-in account - Local System
Finally, here's my query:
SELECT * FROM OPENROWSET(
'Microsoft.ACE.OLEDB.16.0'
,'Excel 12.0;Database=C:\Temp\Test.xlsx;HDR=YES'
,'SELECT * FROM [Sheet1$]')
Sadly, OPENROWSET is know to be buggy for ACE. So there are a bunch of different things to try when trying to tweak it. Adding and removing a few of the following suggestions will hopefully do it for you:
1) xlsx files need to be called with Excel 12.0 Xml;
2) HDR=YES; Tells if a header record is present. It'll crash if there is and you have numerics. Change to NO if there is no header.
3) IMEX=1; This allows for mixed numeric and text fields. In case people start mixing letters in with your numerics.
4) There can be registry problems based on ACE 12.0 vs ACE 14.0. Double check your version.
5) Lastly, weirder than weird... OPENROWSET crashes when you have comments (or don't have comments). If you have --comment, by putting a space after the dashes, -- comment it sometimes helps. So if you have comments, try removing them or rearranging them and you may get a surprise. (I know... it's really a sad state of affairs.)
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0 Xml;HDR=YES;IMEX=1;Database=c:\Users\v-bimoss\Source\Workspaces\External\Onesite\URLoadDB\Templates\T1-Current.xlsx', 'SELECT * FROM [Sheet1$]')
Anyway, since OPENROWSET can be a bit unstable, you may want to import data using a different approach with staging tables, or csv files, etc. But if you get it working, it can be quite nice.
Hope that helps :)
I would check into the steps in these two but in particular checking and changing, if you can, the user who runs SQL Server:
Cannot create an instance of OLE DB provider Microsoft.Jet.OLEDB.4.0 for linked server null
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)"
Like you, I tried everything and I was near to despair when I discovered that the sheet in the excel file must have a name, and it must be referenced with a DOLLAR sign, like [mysheet$].
So here is my openrowset statement that finally works:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.16.0', 'Excel 12.0;Database=\\svr\dir\myfile.xlsx;HDR=YES', 'SELECT * FROM [mysheet$]')
We are using SQL Server 2014 64-bit, using latest ACE provider (Version 16).

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.

Openrowset function failure

I'm executing this openrowset function:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\Users\JCPABALAN\Desktop\Data Migration\ListOfDiscards.xlsx;HDR=YES',
'SELECT * FROM [Sheet1$]')
But it gave me the following error
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.".
Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
I have already set the Ad Hoc Distributed Queries into 1 and I Installed Microsoft ACE and also executed this line of code:
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
GO
But it still won't work.
You Error message has two parts :
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.".
And
Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
That the second part is because of error of first part, So your main error is at first part, that tells us:
The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.
Some common causes and solutions are these:
Path not exists : File or Path C:\Users\JCPABALAN\Desktop\Data Migration\ListOfDiscards.xlsx is not exist;
Note : SQL Server C:\ is referring to the \\Server\C$, So if you are using SSMS and you register a Server and are connecting to it, your path is not exist.
File permission is denied : File or Path C:\Users\JCPABALAN\Desktop\Data Migration\ListOfDiscards.xlsx have security level that you can access them from SQL Server service account, You can grant access to SQL Server service account.
Sheet Name is invalid : Worksheet Sheet1 is not a valid sheet name in your workbook sheets, You maybe change its name.

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