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

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.

Related

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

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.

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.

character encoding for SQL Server to Oracle linked server

This linked server worked fine before we upgraded from SQL Server 2005 to 2008R2, but now it throws this error when querying from certain tables (it still works for other tables):
"linked server "PROD" reported an error. The provider did not give any information about the error...Cannot fetch a row from OLE DB provider "OraOLEDB.Oracle" for linked server "PROD".
I can narrow the problem to one row and when I run this query for that row I get a different error:
select * from openquery( PROD, 'SELECT ID, NAME FROM ITEMS WHERE ID = 5437')
Error:
OLE DB provider "OraOLEDB.Oracle" for linked server "PROD" returned message "01".
OLE DB provider "OraOLEDB.Oracle" for linked server "PROD" returned message "ORA-29275: partial multibyte character".
And I can query the offending NAME column as a DUMP, like this:
select * from openquery( PROD, 'SELECT DUMP(NAME) FROM ITEMS WHERE ID = 5437')
Which returns:
Typ=1 Len=16: 77,73,88,84,69,67,79,32,68,69,32,84,73,68,65,193
then rebuild using SELECT CHAR(77) + CHAR(73) + ..., and I get "MIXTECO DE TIDAƁ". Bottom line, it seems, is that CHAR(193) in the Oracle data is causing my query to fail. But how to fix?
Oracle (https://forums.oracle.com/forums/thread.jspa?threadID=551784) provides this mysterious clue:
ORA-29275: partial multibyte character
Cause: The requested read operation could not complete because a partial multibyte character was found at the end of the input.
Action: Ensure that the complete multibyte character is sent from the remote server and retry the operation. Or read the partial multibyte character as RAW.
However, I don't know how to "Ensure..." and I don't know how to "read... as RAW".
SQL Server is a 64-bit version running on a 64-bit windows server 2008R2 system and has the 64-bit Oracle 11gR2 client installed.
column in SQL: NAME nvarchar(60) NULL
column in Oracle: NAME varchar2(60)
In SQL, sp_helpsort returns:
Latin1-General, case-insensitive, accent-sensitive, kanatype-insensitive, width-insensitive for Unicode Data, SQL Server Sort Order 52 on Code Page 1252 for non-Unicode Data
In Oracle, the NLS_CHARACTERSET is: AL32UTF8
Any help re: why this is not working or how to get this working? Let me know if need further info.
The 193 stored in the Oracle database is not a valid character in the UTF-8 character set. UTF-8 encodes the first 128 characters (0-127) using a single byte but anything beyond 7-bit ASCII requires two or more bytes of storage. Whatever application inserted this data appears to be doing so incorrectly, most likely because it is misconfigured to bypass the character set conversion that is supposed to happen when data is transferred between the client and the database.
What language/ framework/ API is the application that inserted the data into the Oracle database using? What is the client NLS_LANG parameter?

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