I am trying to fetch and insert the data from one table to another on a linked server.
with datum as
(
select * from openquery([DDL_64],'SELECT e.*
, ib.sales_order_number IB_SALES_ORDER_NUMBER
, ib.sales_order_date IB_SALES_ORDER_DATE
, ib.sales_order_type IB_SALES_ORDER_TYPE
, ib.model_number IB_MODEL_NUMBER
, ib.product_item_number IB_ITEM_NUMBER
, ib.product_item_description IB_MODEL_DESC
, ib.install_base_status IB_STATUS
, ib.cs_customer_name IB_Install_Customer
, ib.install_at_country_name IB_Install_Country
, ib.model_so_line_num IB_Model_SO_Line_Number
, current_date last_Run_Date
FROM ws_sls_core.dd_enclosures_forarsdashboard e
left join ws_sls_core.w_customer_ib_profile ib
ON (e.encl_serial = ib.serial_number and ib.model_number not in (''NA'',''UN'')) limit 10')
)
insert into openquery([DDL_64],'select sn1,encl,encl_model,encl_serial,encl_slots,encl_state,encl_oem_name,encl_owm_value,parsed_ts,disk,disk_slot,disk_manf,disk_capacity,disk_sn,disk_type,disk_firmware,disk_model,id,asupid,sn,epoch,external,model,model_str,sn_valid,eth0mac,tz,lic_feature,lic_key,ib_sales_order_number,ib_sales_order_date,ib_sales_order_type,ib_model_number,ib_item_number,ib_model_desc,ib_status,ib_install_customer,ib_install_country,ib_model_so_line_number,last_run_date
from ws_sls_core.dd_enclosure_ib_f_temp ')
select * from datum
I am getting the error:
OLE DB provider "MSDASQL" for linked server "DDL_64" returned message
"the driver cound't identify inserted rows". Msg 7343, Level 16, State
2, Line 1 The OLE DB provider "MSDASQL" for linked server "DDL_64"
could not INSERT INTO table "[MSDASQL]".
I have checked and am using the 64bit version of SSMS & ODBC setup.
PS: Select statement is working fine. I have limited it to 10 rows for testing purposes.
Any input is appreciated. Thank you!
Related
I'm trying to identify new data on a postgres database that does not yet exist on our local SQL Server 2017 so I can download the new data onto our SQL Server. To identify this, I'm connecting to the postgres db using a linked server and then using openquery.
I've inserted the MAX datetime2(7) from our local SQL "events" table into a temp table called #latest:
CREATE TABLE #latest (latest DATETIME2(7))
latest
2023-01-26 14:40:19.1470000
There is a "time" column within the "event" table in the linked server with a datatype of timestamp with time zone. Running this code returns successfully:
DECLARE #Query NVARCHAR(MAX)
SELECT
#Query = '
SELECT
[time],[eventID]
FROM OPENQUERY(
[PostgresServer]
,''
select
"time",
"eventID"
from "event" t
limit 1
''
)'
FROM #latest
EXECUTE sp_executesql #Query
time
eventID
2022-11-17 11:05:17.2450000
730d544e-de4b-47a7-b8d0-80742dc4240d
However, when I try to add a where clause, I get an error:
DECLARE #Query NVARCHAR(MAX)
SELECT
#Query = '
SELECT
[time],[eventID]
FROM OPENQUERY(
[PostgresServer]
,''
select
"time",
"eventID"
from "event" t
where "time" > '[latest]'
limit 1
''
)'
FROM #latest
EXECUTE sp_executesql #Query
Msg 102, Level 15, State 1, Line 33
Incorrect syntax near 'latest'.
I added + to each side of [latest] but then I started getting operator errors:
The data types varchar and datetime2 are incompatible in the add operator.
So I've ended up with the below:
where "time" > '+CAST([latest] AS NVARCHAR(100))+'
But I get this error:
Msg 7399, Level 16, State 1, Line 22
The OLE DB provider "MSDASQL" for linked server "PostgresServer" reported an error. The provider did not give any information about the error.
Msg 7350, Level 16, State 2, Line 22
Cannot get the column information from OLE DB provider "MSDASQL" for linked server "PostgresServer".
Is there a better way I can identify new data on the linked server? Or fix the error?!
I think I've found the answer here: SQL Server : use datetime variable in openquery
needed to add a varchar variable instead of using the datetime column in my temp table :)
I have tried to make an entry into a PostGIS Store in Geoserver using a SQL View in Geoserver with the following statement:
INSERT INTO marker_shp(gid, id, geom)
VALUES ((SELECT max(gid) FROM marker_shp)+1, (SELECT max(id) FROM marker_shp)+1, ST_GeomFromText('POINT (499724.5561430572 5268410.813606716)', 32633))
I got the following error message:
ERROR: Syntax error at »INTO« Position: 23
However, the same Query works fine if I use the same SQL Statement in pgAdmin.
If someone could help me out here I would be very thankful.
All the best:
Thomas :)
The syntax should be
INSERT INTO marker_shp(gid, id, geom)
SELECT max(gid) , max(id)+1 F, ST_GeomFromText('POINT (499724.5561430572 5268410.813606716)', 32633))
FROM marker_shp
--DB2 version 10 on AIX
I have a stored procedure, which I need to update. And want to check if there is data for a certain date. If data exists, go on, else run insert and then go on.
IF (SELECT COUNT(*)
FROM SCHEMA1.TABLE1_STEP1
WHERE XDATE = '9/27/2014' < 1)
THEN (INSERT INTO SCHEMA1.TABLE1_STEP1 (SELECT * FROM SCHEMA2.TABLE2 FETCH FIRST 2 ROWS ONLY))
END IF;
This errors-out.
DB2 Database Error: ERROR [42601] [IBM][DB2/AIX64] SQL0104N An unexpected token "(" was found following "/2014') < 1) THEN". Expected tokens may include: "". SQLSTATE=42601
Any thoughts on what's wrong?
I'm guessing you probably want the less than sign outside of the parenthesis...
However, as an aside, you can also do this kind of statement without an IF (although, I don't have an AIX DB2 available to check for sure. It worked on DB2 for z/OS and LUW, however):
INSERT INTO SCHEMA1.TABLE1_STEP1
SELECT *
FROM SCHEMA2.TABLE2
WHERE NOT EXISTS (
SELECT *
FROM SCHEMA1.TABLE1_STEP1
WHERE XDATE = '9/27/2014'
)
FETCH FIRST 2 ROWS ONLY
Also, you're not providing an ORDER BY on the SCHEMA2.TABLE2 select, so your results could come back in any order (whatever is "easiest" for the database engine)... order is not guaranteed unless you provide the ORDER BY statement.
i have the following which queries a linked server i have to talk to.
SELECT
*
FROM
OPENQUERY(DWH_LINK, 'SELECT * FROM TABLEA ')
It will typically return most of the data but some rows are missing?
The linkeds server is coming from an oracle client
Is this a problem anyone has encountered w/ openquery?
I had exactly the same problem.
The root cause is that you've set up your linked server using ODBC instead of OLE DB.
Here's how I fixed it:
Delete the linked server from SQL Server
Right click on the "Linked Servers" folder and select "New Linked Server..."
Linked Server: enter anything..this will be the name of your new linked server
Provider: Select "Oracle Provider for OLE DB"
Product Name: enter "Oracle" (without the double quotes)
Data Source: enter the alias from your TNSNAMES.ORA file. In my case, it was "ABC.WORLD" (without the double quotes)
Provider String: leave it blank
Location: leave it blank
Catalog: leave it blank
Now go to the "Security" tab, and click the last radio button that says "Be made using this security context:" and enter the username & password for your connection
That should be it!
This seems to be related to the underlying provider capabilities and others have also run into this and similar size/row limitations. One possible work-around would be to implement an iterative/looping query with some filtering built in to pull back a certain amount of rows. With oracle, I think this might be using the rownum (not very familiar with oracle).
So something like
--Not tested sql, just winging it syntax-wise
SELECT * FROM OPENQUERY(DWH_LINK, 'SELECT * FROM TABLEA where rownum between 0 AND 500')
SELECT * FROM OPENQUERY(DWH_LINK, 'SELECT * FROM TABLEA where rownum between 500 AND 1000')
SELECT * FROM OPENQUERY(DWH_LINK, 'SELECT * FROM TABLEA where rownum ...')
BOL:
link
This is subject to the capabilities of the OLE DB provider. Although the query may return multiple result sets, OPENQUERY returns only the first one.
I had this same problem using the Oracle 10 instant client and ODBC. I used this client as I am connecting to an Oracle 10gR2 database. I opened a ticket with Microsoft support and they suggested using the Oracle 11 instant client. Surprise! Uninstalling the 10g instant client, installing the 11g instant client and rebooting resolved the issue.
Ken
I had exact same problem with an SQL 2014 getting data from SQL 2000 through OPENQUERY. Because ODBC compatibility problem, I had to keep generic OLE DB for ODBC driver. Moreover, the problem was only with SQL non-admin account.
So finally, the solution I found was to add SET ROWCOUNT 0:
SELECT * FROM OPENQUERY(DWH_LINK, 'SET ROWCOUNT 0 SELECT * FROM TABLEA ')
It seems the rowcount might been change somewhere through the SQL procedure (or for this user session), so setting it to 0 force it to return "all rows".
I am writing a T-SQL program over a DB2 database on a LINUX box (DB2/LINUXX8664) using a linked server. I think the DB2 is Version 9.5.3 but not certain. I am receiving an error that I feel is likely a DB2 issue as the syntax checks out okay in T-SQL. This is the code:
IF(SELECT(OBJECT_ID('TEMPDB..#TempFile))) IS NOT NULL DROP TABLE #TempFile
SELECT *
INTO #TempFile
FROM OPENQUERY(LinkedServer, '
SELECT F.LOAN_NUMBER,
(SELECT
SUM(EXP_CHILD_CARE_AMOUNT) + SUM(EXP_FOOD_AMOUNT) +
SUM(EXP_LIFE_INSURANCE_AMOUNT) + SUM(EXP_TRANSPORTATION_AMOUNT) + SUM(EXP_TUITION_AMOUNT)+
SUM(EXP_USER_1_AMOUNT) + SUM(EXP_USER_2_AMOUNT) + SUM(EXP_USER_3_AMOUNT) +
SUM(EXP_UTILITIES_AMOUNT)
FROM FINANCIAL F)
AS ExpenseTotal,
(SELECT
SUM(MORTGAGOR_NET_PAY_AMOUNT) + SUM(MORTGAGOR_OTHER_INCOME_AMOUNT) AS IncomeTotal
FROM FINANCIAL F
INNER JOIN BDE.LOAN_V a ON F.LOAN_NUMBER = A.LOAN_NUMBER)
WHERE A.FIRST_PRINCIPAL_BALANCE> 0
GROUP BY F.LOAN_NUMBER
ORDER BY F.LOAN_NUMBER,
FETCH ONLY WITH UR ')
Here is the error:
OLE DB provider "MSDASQL" for linked server "LINKEDSERVER" returned message "[IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "A" was found following "BER) WHERE". Expected tokens may include: "FROM". SQLSTATE=42601
".
Msg 7350, Level 16, State 2, Line 4
Cannot get the column information from OLE DB provider "MSDASQL" for linked server "LINKEDSERVER".
= A.LOAN_NUMBER)
WHERE A.FIRST_PRINCIPAL_BALANCE> 0 - that bracket looks out of place.
The first thing I notice is that you have a naked query, which is not uncommon in Microsoft SQL Server. A simplified version of your linked query looks like this:
SELECT (subquery), (subquery) WHERE ...conditions...
In DB2, you must have a FROM clause in any query. Microsoft and some other SQL vendors permit a SELECT with no FROM clause, but this isn't standard SQL. In this case, DB2 conforms to the standard.
Second thing I notice:
IF(SELECT(OBJECT_ID('TEMPDB..#TempFile))) IS NOT NULL DROP TABLE #TempFile
Do you need to close that quoted string?