Openrowset from CSV file - sql-server

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

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.

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

UPDATE & DELETE queries through Linked server in fails. But INSERT & SELECT works fine

I have a Windows 2019 machine. I have SQL Server 2016. I have created a linked server & I am trying to execute some queries on a remote Db2 server. I am using MSDASQL provider, i.e. 'Microsoft OLE DB Provider for ODBC Drivers'.
The INSERT & SELECT queries work fine.
However the UPDATE & DELETE queries fail. For example, a DELETE query as shown gives the below error.
DELETE OPENQUERY (TEST2016, 'SELECT * FROM ABC.WORK_ITEM WHERE id = 123');
Msg 7399, Level 16, State 1, Line 12.
The OLE DB provider "MSDASQL" for linked server "TEST2016" reported an error. The provider did not give any information about the error.
Msg 7320, Level 16, State 2, Line 12.
Cannot execute the query "SELECT * FROM ABC.WORK_ITEM WHERE id = 123" against OLE DB provider "MSDASQL" for linked server "TEST2016".
As I mentioned, if I run the SELECT query separately, as shown below, the query works fine.
select * from OPENQUERY(TEST2016, 'SELECT * FROM ABC.WORK_ITEM where wi_id = 123');
Am I missing any configuration/settings?

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.

DBLink from SQL Server to postgresql: get sequence nextval

I've got a SQL Server 2008 that needs to connect to a POSTGRESQL 9.0 server.
One of the tasks that i need is to get the next value of a sequence over the POSTGRESQL
Example 1 :
execute ('select nextval( ''cadastro.pessoa_seq'' )' ) at POSTGRESQL ;
error:
Msg 7215, Level 17, State 1, Line 1 Could not execute statement on
remote server 'POSTGRESQL'.*
Example 2 :
select * from openquery( POSTGRESQL ,'select nextval( ''cadastro.pessoa_seq'' )')
error:
Msg 7399, Level 16, State 1, Line 1 The OLE DB provider "MSDASQL" for
linked server "POSTGRESQL" reported an error. The provider reported an
unexpected catastrophic failure. Msg 7350, Level 16, State 2, Line 1
Cannot get the column information from OLE DB provider "MSDASQL" for
linked server "POSTGRESQL".*
None of these options works!
Is there any good alternative ?
Thank you in advance
dealing with ODBC sources and postgres I have to do this:
nextval(''cadastro.pessoa_seq''::regclass) for it to work
Perhaps your Postgresql connection is readonly , have you tried to execute an update/insert request ?
nextval update the sequence , try:
currval(''cadastro.pessoa_seq''::regclass)

Resources