sql opendatasource need to access files on a mapped network drive - sql

I have a number of files in different formats (Excel, Access, and CSV) saved on a network drive and I need to use opendatasource and openrowset to insert the data into a SQL table. These queries will only work if the files are saved on the SQL server. Is there a way to get around this?
I have admin rights on my local machine and on the server with full read/write permissions. The drives are also mapped on the server. Here are the queries and the error messages that I am getting:
SELECT Seq, Label
FROM OPENDATASOURCE ('Microsoft.ACE.OLEDB.12.0','Data Source = "G:\IMA Utilization Reports\\IMAv2k.mdb"' )...BackupLabels
Results in: OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "'G:\IMA Utilization Reports\DB\IMAv2k.mdb' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.".
SELECT Seq, Label
FROM OPENDATASOURCE ('Microsoft.ACE.OLEDB.12.0','Data Source = "\\172.16.0.32\Finance\IMA Utilization Reports\DB\IMAv2k.mdb"' )...BackupLabels;
Results in: OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Access database engine cannot open or write to the file '\172.16.0.32\Finance\IMA Utilization Reports\DB\IMAv2k.mdb'. It is already opened exclusively by another user, or you need permission to view and write its data.".
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'excel 12.0;HDR=yes;imex=1;driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)}; extended properties=excel 12.0 xml;
Database=G:\IMA Utilization Reports\Provider ID Numbers.xlsx',
'select * from [Employee ID Numbers$]')
Results in: Msg 7399, Level 16, State 1, Line 32
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 32
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
If anyone has an answer or can point me to somewhere that does I would greatly appreciate it. Thanks in advance.

Related

How to allow all users to run this SQL query?

Some context: I am trying to upload data from Comcast into our database. Comcast sends the data in a macro-enabled workbook with file extension ".xlsm" which is disgusting. To circumvent this, I setup a power automate desktop flow to open the XLSM files and save them as XLSX files. I then run this query under an INSERT on our reporting server to get the data into the DB:
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;Database=\\COMPANYNAME\company\customer\Comcast\Comcast.xlsx;', Sheet1$);
The only problem is, I'd like this to be automated. So I setup a cloud power automate flow to run the desktop flow and then execute a stored procedure in SQL Server. My flow receives the same error I do on my machine as well as any user outside of the COMPANYNAME\administrator session in SSMS on our reporting server:
Msg 7399, Level 16, State 1, Line 5
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 5
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
It has been very difficult to research this issue as there seem to be many possible causes. I'm no IT wizard but I know enough to be dangerous. Thank you in advance for your time and help and I eagerly await your response!

Connect to SQL Azure DB using OpenRowSet from on-premise SQL Server

I am trying to connect to SQL Azure database from an on-premise SQL Server using openrowset, but it is failing with an error.
My query is
SELECT a.*
FROM OPENROWSET('SQLNCLI', 'Server=sqlazureserver.database.windows.net;Database=dbname;User ID=username;Password=password;Connection Timeout=30;', 'select * from [dbo].[tablename]') AS a;
and the error I get:
Msg 7399, Level 16, State 1, Line 11
The OLE DB provider "SQLNCLI11" for linked server "(null)" reported an error. Authentication failed.
Msg 7303, Level 16, State 1, Line 11
Cannot initialize the data source object of OLE DB provider "SQLNCLI11" for linked server "(null)".
I am able to successfully connect using linked server, but I do not want to go that route as my connection strings will be dynamic.
From documentation ,I could see open row set is not supported against SQL Azure database as of now.
Instead of openrowset,you can use distributed queries to accomplish the same
exec ('select * from table') at linkedserver

Importing Excel 2007 File Using SQL Server 2005

I have an excel 2007 file with about 301808 rows and 2 columns. I was trying to use SSIS to import but cant use 2007 excel. I then decided to try and make a linked server in SQL Server, Following the instructions here:
In SQL Server Management Studio, expand Server Objects in Object Explorer.
Right-click Linked Servers, and then click New linked server.
In the left pane, select the General page, and then follow these steps:
In the first text box, type any name for the linked server.
Select the Other data source option.
In the Provider list, click Microsoft Jet 4.0 OLE DB Provider.
In the Product name box, type Excel for the name of the OLE DB data source.
In the Data source box, type the full path and file name of the Excel file.
In the Provider string box, type Excel 8.0 for an Excel 2002, Excel 2000, or Excel 97 - workbook.
Click OK to create the new linked server.
From Here: http://support.microsoft.com/kb/306397/EN-US
However the instructions only covers previous excel files not 2007 so I looked up connection strings for 2007 and used Excel 12.0 instead of Excel 8.0 in settings of linked server:
Provider: Microsoft Office 12.0 Access Database Engine OLE DB
Provider Product Name: Excel Data source: C:\Documents and
Settings\UserName\Desktop\Main\FilesIns\MyExcelFile.xlsx Provider
String: Excel 12.0
Location:
Catalog:
I then got this error:
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "EXCEL_MY_FILE" does not contain the table "Report 1$". The table either does not exist or the current user does not have permissions on that table.
I went into security and added nt authority\system just in case it was permissions problem and it still gave me the error above.
The excel table is called 'Report 1'.
I also tried using openrowset in sql to see what happened and I got this:
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server.
I then tried the code in link below to see if it helped me use openrowset:
How to enable Ad Hoc Distributed Queries
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
And got:
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "Syntax error in FROM clause.".
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "Select * from C:\Documents and Settings\UserName\Desktop\Main\FilesIns\MyExcelFile.xlsx" for execution against OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
So I am not sure if the values in my openrowset statement are correct.
Really just looking for a way to import this file into an sql table without using SSIS - I cant use it anyway.
Thanks
Andrew
Under your sp_Reconfigure, see that you reference an article here.. However, I think you may need to go to Microsoft instead..
http://technet.microsoft.com/en-us/library/ms187569%28v=sql.90%29.aspx
And there are quite a few examples of how to use OPENROWSET here -
http://technet.microsoft.com/en-us/library/ms190312%28v=sql.90%29.aspx

Problem with update sql with excel

I have a problem with this query:
UPDATE Provinces
SET Provinces.DefaultName=T2.Defaultname
FROM Provinces
INNER JOIN
OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\provinces.xlsx;HDR=YES',
'SELECT Code, Defaultname FROM [Arkusz1$]') T2
On Provinces.Code = t2.Code
WHERE Provinces.Code = T2.Code
I get error:
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "Microsoft.Jet.OLEDB.4.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.Jet.OLEDB.4.0" for linked server "(null)".
What is the source of this error, and how can I modify the SQL statement?
There are several possible causes of this detailed here: Linked Server using Microsoft.Jet.OLEDB.4.0 problem.
A likely is cause is file system permissions of the temp directory for the sql service login for whoever is accessing that linked server: C:\Documents and Settings\(sql login name)\Local Settings\Temp
That is permission denied error. Follow:
How to import data from Excel to SQL Server
How to use Excel with SQL Server linked servers and distributed queries
Note If you are using SQL Server 2005, make sure that you have enabled the Ad Hoc Distributed Queries option by using SQL Server Surface Area Configuration.
sp_configure 'Ad Hoc Distributed Queries', 1

how to import data from DBF to SQL using SQL script?

I am trying to import data from DBF file to SQL table using the following command -
select *
from openrowset('MSDASQL',
'Driver={Microsoft dBase Driver (*.dbf)};DBQ=E:\data\;',
'select * from E:\data\a.dbf')
But it is failing saying
OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC dBase Driver]General Warning Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x120 Thread 0x3084 DBC 0x303dfbc Xbase'.".
OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC dBase Driver] Disk or network error.".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)".
Any clue why it is giving error? For the second error I have given full access to temp folder as suggested by some blog. Still it is showing both error.
If filename is a .dbf
Try this:
select * from
openrowset('MSDASQL',
'Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=E:\Data;',
'SELECT * FROM a')
Your SELECT is specifying the path to the file - you should be specifying the table name instead, which is presumably 'a'.
To supplement CodeByMoonlight, the Driver information points to the path the data file is found, so then your subsequent query should only be "Select * should from a" since both the path and the .dbf extension should be implied
You are using 64-bit SQL Server which is looking to the 64-bit ODBC sources when the drivers are 32-bit. There is a workaround for FoxPro (also DBF) that should work for dBASE as well:
Open a new SqlDataSource in Visual Studio using the .NET Framework Provider for OLE DB then MS OLE DB Provider for VFP. The connection string should look like this (or with UNC)
Provider=VFPOLEDB.1;Data Source=h:\Programs\Data;Persist Security Info=True;User ID=Your_user;Password=your_password
The config wizard includes a part to test your SQL. You can use this run ad hoc SQL.