SQL OpenRowSet Query OLE DB not valid - sql-server

I have been trying to import an Excel file into my SQL Server but I seem to get this error:
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)"
returned message
"'C:\Windows\system32\Users\Desktop\Folder_name\Update_File.xlsx' 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.".
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)".
This is the following query I used in order to import the file:
Select * Into EXCEL_IMPORT
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0; Database=C:Users\Desktop\Folder_name\Update_File.xlsx; HDR=YES; IMEX=1', 'SELECT * FROM [Sheet1$]');
I am new to SQL so I have no idea if I have everything correctly installed or if I need anything else.
Your help is much appreciated! :)
Thank you!

That path is surely not valid. Would you try (still doesn't look right to me that you have a path named liked that at all but want to believe you):
Select * Into EXCEL_IMPORT
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0; Database=C:\Users\Desktop\Folder_name\Update_File.xlsx; HDR=YES; IMEX=1', 'SELECT * FROM [Sheet1$]');

Related

T-SQL Openquery - error due to space in the table name

I have the below query that is trying to pull data from Sage 50 pervasive 13 database into SQL server using a link server. I've been able to pull all data from all tables into SQL Server except for this one table because it has a space in the table name.
I've not been successful with anything I've changed it to. Can anyone help me get this query working?
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1.Budget Details')
When I change the above query to this:
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1.[Budget Details]')
I get this error message:
OLE DB provider "MSDASQL" for linked server "ARKSAGE" returned message "[PSQL][ODBC Client Interface][LNA][PSQL][SQL Engine]Syntax Error: select * from NEPHROPATHOLOGYASSO1.<< ??? >>[Budget Details]".
Msg 7321, Level 16, State 2, Line 61
An error occurred while preparing the query "select * from NEPHROPATHOLOGYASSO1.[Budget Details]" for execution against OLE DB provider "MSDASQL" for linked server "ARKSAGE".
The PSQL in the error message tells me the linked server is probably running Postgresql, rather than SQL Server. Postgresql marks object names with double quotes instead of brackets. Therefore you should try this:
select *
from openquery(ARKSAGE,'select * from NEPHROPATHOLOGYASSO1."Budget Details"')
Additionally, I'm not sure what the << ??? >> text is for, but it looks a little like it's complaining about an odd unicode character in there somewhere. So look out for invisible whitespace. Or maybe it's just part of how the error message is formatted in the context of a linked Postgresql server.

Use openrowset to read txt file in SQL Server

I tried the following query:
select *
from openrowset('MADASQL'
,'Driver={Microsoft Text Driver (*.txt; *.csv)}'
,'select * from C:\test.txt')
but it failed with an error:
Cannot initialize the data source object of OLE DB provider "MSDASQL" for linked server "(null)"
So I tried:
SELECT *
FROM OPENROWSET(BULK 'C:\test.txt',
FORMATFILE= 'C:\test.xml') AS a
it failed with
Cannot bulk load. The file "C:\test.txt" does not exist
I have check the path and find nothing wrong.

Importing Excel Data into SQL Server 2008 R2 Express

There seems to have been many discussions on this but I couldn't find something specific to what I am looking for. I am trying to use a query to import data from Excel to an existing table in SQL Server.
My query is:
INSERT INTO DailyRawData
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=NO;
Database=C:\Users\home\Desktop\SQLImportTrim.xls', [data$]);
I get the following error:
Msg 7314, Level 16, State 1, Line 2
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" does not contain the table "data$". The table either does not exist or the current user does not have permissions on that table.
I don't think this is a permission issue for me as I am set up as SysAdmn. I am wondering if the error is due to the last part of the query [data$] since this is what the error msg refers to. FYI the name of the excel file is SQLImportTrim and the tab that contains all my data is named data. There is no table named data in my Excel file. Is my query correct?
you don't use [data$] you use the name of the sheet, so the standard starting point is usually [Sheet1$] if you haven't renamed it.

Openrowset from CSV file

I am trying to read from a CSV file located on shared folder on the network but getting error for the following code:
SELECT * INTO #TempTbl
FROM OPENROWSET
('SQLNCLI11',
'ServerName = <username>;Trusted_Connection=yes;',
'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=\\sharedpath\DIR path\;
SELECT * FROM CSV_File.csv) as Test
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ','.
When I remove the server line like this:
SELECT * INTO #TempTbl
FROM OPENROWSET
('SQLNCLI11',
--'ServerName = <username>';Trusted_Connection=yes;',
'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=\\sharedpath\DIR path\;
SELECT * FROM CSV_File.csv) as Test
I get the following error:
OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Invalid authorization specification".
OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Invalid connection string attribute".
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "SQLNCLI11" for linked server "(null)" reported an error. Authentication failed.
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "SQLNCLI11" for linked server "(null)".
What does that mean? Does this error message indicate something wrong in my script or is access permission issue? I was able to read from a table on the server suing the same openrowset connection when I no longer use the CSV driver.
I am not sure what to do in order to get this openrowset to read the CSV file. Any clue? Do you think it has to do with the server account does not have access to the account I am using does not have access to the shared folder?
You are using the wrong provider. Your connection string is written for connecting to a server (sort of). You should be using the bulk option for OPENROWSET per example E on this page.
It would look something like this, although you need to create a format file per the instructions here.
SELECT *
FROM OPENROWSET( BULK '\\sharedpath\DIR path\CSV_File.csv',
FORMATFILE = '\\sharedpath\DIR path\CSV_format.fmt');

Getting syntax error when trying to use OPENQUERY

I am trying to do a query via ODBC to our ERP database. The documentation guide suggests that we use OPENQUERY to send the query.
Here is my example query
SELECT
Q.Part_No,
Q.[Description],
Q.Part_Type
FROM OPENQUERY
(
LINKEDSERVER,
'
SELECT
P.Part_No,
P.[Description],
P.Part_Type
FROM LINKEDSERVER...Part_V_Part AS P
WHERE P.Part_No = ''2712768''
'
) AS Q
When I try to run that query though I get the following error
OLE DB provider "MSDASQL" for linked server "LINKEDSERVER" returned message "[LINKED][ODBC Plex ODBC Report Data Source driver][OpenAccess SDK SQL Engine]Syntax error in SQL statement. syntax error line 1 at or after token <LINKEDSERVER>.[0]".
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "
SELECT
P.Part_No,
P.[Description],
P.Part_Type
FROM LINKEDSERVER...Part_V_Part AS P
WHERE P.Part_No = '2712768'
" for execution against OLE DB provider "MSDASQL" for linked server "LINKEDSERVER".
Can anyone help me here? I've never used OPENQUERY before, but I'm coping the example straight as it is in the example documentation.
Should be like this
SELECT
Q.Part_No,
Q.[Description],
Q.Part_Type
FROM OPENQUERY
(
LINKEDSERVER,
'
SELECT
P.Part_No,
P.[Description],
P.Part_Type
FROM DatabaseName.SchemaName.Part_V_Part AS P
WHERE P.Part_No = ''2712768''
'
) AS Q
Replace DatabaseName and SchemaName with your actual database name and schemaname (probably dbo)
You don't need the linked server name inside the query
Take a look at Having Fun With OPENQUERY And Update,Delete And Insert Statements for some examples

Resources