Getting syntax error when trying to use OPENQUERY - sql-server

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

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.

Insert Statement error returned message "the driver cound't identify inserted rows"

I am transferring the data from the local SQL server to a remote (PostgreSQL) table using a Linked server (ODBC). This works perfectly as long as I stick with Select query but fails with Insert, Delete, etc.
Error :
OLE DB provider "MSDASQL" for linked server "DDL_PUYAML1_64" returned
message "the driver cound't identify inserted rows". Msg 7343, Level
16, State 2, Line 75 The OLE DB provider "MSDASQL" for linked server
"DDL_PUYAML1_64" could not INSERT INTO table "[MSDASQL]".
I have tried with Single Value but nothing changes.
As suggested Error messages when you perform an UPDATE, INSERT, or DELETE Transact-SQL statement on a remote table by using the OpenQuery function: "7357" and "7320" here, I have tried with column names instead of "*" and tried putting where 1=0 as well.
Any suggestion/feedback is appreciated.
Thank you!
EDIT: PFB the code
Insert openquery([DDL_PUYAML1_64],'select sn1,encl,encl_model from ws_sls_core.dd_enclosures_forarsdashboard_v1 where 1=0 ')
select sn1,encl,encl_model from #temp1

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?

Error while querying SAP HANA from SQL Server 2014

The query below is working fine:
SELECT *
FROM OPENQUERY(DS_64, 'SELECT TOP 1 * FROM "workforce"."sales" WHERE ORDER_UPDATED > ''2020-07-24 14:03:23'' ')
When I'm using ORDER_CREATED in place of ORDER_UPDATED, I'm getting the below error:
OLE DB provider "MSDASQL" for linked server "DS_64" returned message
"[SAP AG][LIBODBCHDB DLL][HDBODBC] General error;314 numeric overflow:
search table error: [6944] exception 70006944: AttributeEngine:
overflow in numeric calculation
Any help is going to be highly appreciated. Thanks in advance :)
Below is the query that is giving the above error:
SELECT *
FROM OPENQUERY(DS_64, 'SELECT TOP 1 * FROM "workforce"."sales" WHERE ORDER_CREATED > ''2020-07-24 14:03:23'' ')
I think the syntax for top is limited in the HANA universe. Also, check the datatype HANA is actually using from HANA Studio.

Find Column Data type in linked server

I just started working on a new project where data is stored in linked server. I have to add new columns to a table in staging environment, which already exists under same table name in linked server.
I have dumped data into a temp table but all the columns with data type decimals, int and numerics are converted into float in staging database. Also, when I'm trying to access information_schema or sys.columns for metadata getting below error. Any workaround to see the data type of table columns in linked server ?
select * from openquery(LINKEDSERVER,'select * from information_schema.columns')
Error:
OLE DB provider "MSDASQL" for linked server "LINKEDSERVER" returned message "[LINKEDSERVER][ODBC 64bit driver][OpenAccess SDK SQL Engine]Base table:columns not found.[10129]".
Msg 7321, Level 16, State 2, Line 45
An error occurred while preparing the query "select * from information_schema.columns" for execution against OLE DB provider "MSDASQL" for linked server "LINKEDSERVER".
Thanks in advance.
If you can query the linked server, then the following would work:
select
*
from [LinkedServerInstanceHere].[DatabaseOnLinkedServerHere].information_schema.columns
Just populate your details in [ ]...
I just tested it and it works on my side.
Hope this helps.

Resources