Querying DBF from SQL Server 2008-2012 - sql

I've the following problem, we have an old fox pro app running in an SCO UNIX server (I know this is amazing!), we actually transfer files from FTP to differents server then we transfor them from dbf to a txt and then! we read the txts and we finally save them in a SQL.
The above situation is awfull i know, now we have a lot of new apps, but this in particular cause us a lot of problems, and we are buying a new solution, but meanwhile, we need to deal with this.
So we have SQL 2008 Servers and SQL 2012 Servers, and i want to read and modify the DBF directly from the SQL Servers, so I google it and get the following solution.
sp_configure 'show advanced options', 1
reconfigure
GO
sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1;
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1;
SELECT COLUMN1,
COLUMN2
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','dBASE III;Database=\\RemotServerName\ShareName\Folder1\SubFolder1\','SELECT COLUMN1, COLUMN2 FROM DBASEFIL.dbf WHERE COLUMN3 = ''C''')
I Almost cry, but then i get the following error (I translate it from spanish):
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "Unspecified error".
Mens. 7303, Level 16, State 1, Line 5
Can not initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
I Hope you could give me some advice, i tried a lot of solutions with out any result

Try creating a Linked Server in SQL Server to the Foxpro database.

I reached that 7303 problem trying to do the same thing and it was easy to fix. Just make sure the SQL user account has the right permissions to the files you're trying to import.

Related

Excel Linked Server in SQL Server 2008 R2 OLEDB error

I have set up a linked server from SQL Server 2008 R2 to an Excel spreadsheet, from which I read the contents in to a table, for comparison during an import.
The spreadsheet is held on a network share from an application server.
Basically, this works perfectly on the SQL Server when I run:
SELECT * FROM SpreadsheetLink...[CONTENTS$]
However, as I now need to schedule this, I need to create an SSIS package on the application server, which runs the same query.
So now, whenever I try to do this though (either by writing the script in to SSIS or by logging in to SQL Server Mgmt Studio on the App server and then opening an SQL connection to the SQL Server and querying directly) I get an error:
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "SpreadsheetLink" 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 "SpreadsheetLink"
I first wondered whether I had forgotten to install OLEDB12.0 on the application server, but it is there.
I next wondered whether it was a permissions issue, so I added the EVERYONE group, with "Full Control" to the folder containing the spreadsheet and also ensured that the account that I am both logged in with and running the query as is an administrator on both the SQL server and the Application Server.
I also checked that the account was a member of the Distributed COM user group on both servers (it was).
Additional info:
I have tried several things, but it still fails.
I added the following
sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
I then re-tested, but got the same results (success from the local SQL server, failure from the other server).
Next, I tried creating a different query, referring to the spreadsheet directly by path, rather than by Link-name, but that resulted in exactly the same outcome:
SELECT * INTO TargetTableName FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=\\SERVER_NAME\PATH\fileName.xls;HDR=YES',
'SELECT * FROM [sheetName$]')
I also applied all of the suggestions in the following page:
http://visakhm.blogspot.co.uk/2013/12/how-to-solve-microsoftaceoledb120-error.html. But still no joy.

Export Sql server data to Excel by using Query

I am trying to export the SQL server data to the excel file with using the sql query. But i am getting error
OLE DB provider "Microsoft.Jet.OLEDB.4.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.Jet.OLEDB.4.0" for linked server "(null)".
The Query which i am using
select * from OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\share\Test1.xlsx;HDR=YES','SELECT id FROM [Sheet1$]') SELECT id FROM Table
I have also enabled
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE
EXEC master.dbo.sp_MSset_oledb_prop 'Microsoft.Jet.OLEDB.4.0', 'AllowInProcess', 0
GO
EXEC master.dbo.sp_MSset_oledb_prop 'Microsoft.Jet.OLEDB.4.0', 'DynamicParameters', 0
GO
I have also shared my folder with full permissions.
I have Given the Excel file same headers with the table name
But Same Error
The connection you are using is wrong: it is the JET driver which
is for reading/writing flat files of various types
is pointing to a file called c:\share\test1.xlsx
is obsolete, and has been replaced by the ACE drivers.
You need to re-write your code. This article gives a good idea on how to do it. One bit you must NOT miss out, and should do first, is the para titled Referencing the ADO object library.
You may well have a whole load of questions about things the code is doing (for instance, what is INTEGRATED SECURITY=SSPI? and a load more), so many that I'm not going to answer them because I would have to write a chapter of a book to and I don't know whether you would ask any of them. I suggest that you read the article, try the code, and then post individual questions in invividual posts here.
Once clue; this bit of code:
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=pubs;"
...is specifying the database server and the name of the database. Replace the (local) with the name of the database server if it's not your local machine. The pubs name is the name of the database. Replace with your database.
Also, this bit:
' Extract the required records.
.Open "SELECT * FROM Authors"
Here, "SELECT * from Authors" is the SQL which defines the data the query will return. You need to replace this with the SQL which will get the data you want from your database. Again, a rather large subject.

import a DBF file in SQL Server using SQL Script

How can I import a .dbf file into SQL Server using a SQL script?
Found answers from this post, but unfortunately none of them work to me :( :
Trying to Import FoxPro DBF File to SQL Server
and
How to import a DBF file in SQL Server
When I'm trying this code :
SELECT *
INTO [APP_DB]..[BILLHEAD]
FROM OPENROWSET('MSDASQL', 'Driver=Microsoft Visual FoxPro Driver; SourceDB=D:\DBF; SourceType=DBF', 'SELECT * FROM BILLHEAD')
I get this error:
OLE DB provider "MSDASQL" for linked server "(null)" returned message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". Msg 7303, Level 16, State 1, Line 1 Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)".
And also, when trying this :
SELECT *
FROM openrowset('VFPOLEDB','D:\DBF\BILLHEAD.dbf';'';
'','SELECT * FROM BILLHEAD')
I get this error :
Msg 7438, Level 16, State 1, Line 1
The 32-bit OLE DB provider "VFPOLEDB" cannot be loaded in-process on a 64-bit SQL Server.
I don't want to download any third party application. That's why I'm trying all the possible solution and I need your help now guys. I'm creating a small application to import .DBF files into SQL Server.
Regards,
You are using 64-bit SQL sever, but FoxPro OLE DB driver is 32-bit. You need to consult this article which discusses how to use the 32-bit OLE DB driver with 64-bit SQL Server.
Gimo, I'm not sure this will work and I'm no MS SQL Server expert, but I've been wrestling with a similar problem lately and I have an idea. I think you may be able to get that first block of code from your question to work if you execute the following statements first:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE;
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
RECONFIGURE;
GO
This may not work if you don't have adequate permissions (which happened in my situation), but it may be worth a shot.
Our office SQL/GIS guru, Burce, solved a similar problem I was having. I'm not sure of all the details of how he did it, so while I am reluctant to enter this as an "Answer" (it is too many characters to enter as a Comment) I'll describe what I can in case it is helpful for anyone. First be aware that he has full permissions on the SQL Server, so this solution may not be feasible for all DB users to implement. Bruce set up a Linked Server that's connected to a directory ".../DBF/" on our LAN file server. He also set up a similar Linked Server & directory for CSV files. Anyone in our office can simply copy a DBF file to this directory and then access it in SQL Server as if it were a regular table in a SQL Server database. I access this in SSMS by connecting to the Database Engine then going to Server Objects > Linked Servers > "DBF" > Catalogs > default > Tables > file name . The Properties of the Linked Server say the following:
From General tab of Properties window
From Security tab of Properties window
From Server Options tab of Properties window
Note that this may or may not be a secure configuration for all database server environments, but this is on a SQL Server that is on our internal network, only accessible within our office, with no endpoints or access outside our LAN (it's not used as a server for web, or other internet services).
I have had similar problems where stuff just wasn't working trying to move legacy tables from VFP to SQL 2008R2 and used the following procedure:
select table within VFP
copy to blahblah xl5
Step 2 results in an excel file
Use SQL 2008 R2 or higher "Import and Export Data (32 bit)" to import the excel file.
I was running Windows 7 64 bit and still had to use the 32 bit import to make it work smoothly.
This may explain why you need the 32 bit Import: https://msdn.microsoft.com/en-us/library/ms141209.aspx

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