Use openrowset to read txt file in SQL Server - 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.

Related

SQL OpenRowSet Query OLE DB not valid

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$]');

how can i select a table from linked server?

I'm tring to run this SQL query :
select * from openquery(CUIC,'select * from [informix].[agentcalldetailsnapshot]')
I expect a selected table but I have this error message as a result:
Msg 7321, Niveau 16, État 2, Ligne 1
An error occurred while preparing the query "select * from [informix].[agentcalldetailsnapshot]" for execution against OLE DB provider "MSDASQL" for linked server "CUIC".
Thanks for your help !!

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');

Write in Excel spreadsheet from SQL Server linked server

I have a client who has a huge Excel file. They absolutely want to continue to work with this file. They asked us if we can update data in the file from a PocketPC.
I created a linked server to the spreadsheet:
EXEC master.dbo.sp_addlinkedserver
#server = N'ExcelFile',
#srvproduct=N'Excel',
#provider=N'Microsoft.ACE.OLEDB.12.0',
#datasrc=N'Filename.xls',
#provstr=N'Excel 12.0;IMEX=1;HDR=YES;'
I can successfully query the file with the following:
SELECT *
FROM ExcelFile...[Feuil1$]
If the file is already open, I get an error. I guess the file MUST be closed?
Anyway, is there a way to update cells in the Excel file with something like:
UPDATE ExcelFile...[Feuil1$]
SET [BIP] = 123456
WHERE [BIP] = '966985'
I get this error:
An error occurred while preparing the query "UPDATE Feuil1$ set BIP
= (1.234560000000000e+005) WHERE BIP=(9.669850000000000e+005)" for execution against OLE DB provider "Microsoft.ACE.OLEDB.12.0" for
linked server "ExcelFile".
Thanks for your time and help

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