How to find datetime's on postgres linked server greater than datetime2 on local SQL Server using Open Query - postgresql

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 :)

Related

Parametrized OPENQUERY on Firebird database token unknown error message

I've been at this for a little longer then I'd like to admit.
From a SQL Server database, I'm trying to run a dynamic OPENQUERY on a Firebird database, but I'm getting in all kinds of trouble with the datetime condition. Now, I'm trying to use a parametrized approach to circumvent potential issues with the datetime format, but getting a token unknown error. Any advise is appreciated; full details below:
FYI, I know the query is redundant, but I'm just trying to get it to work.
t-sql:
DECLARE #days INT
DECLARE #start DATETIME
DECLARE #end DATETIME
DECLARE #sql nVARCHAR(MAX)
SET #days = 1
SET #end = GETDATE()
SET #start = DATEADD(DAY,-#days,#end)
SET #sql = N'(SELECT OBJID FROM SALE WHERE MODIFIED >= #test1)'
DECLARE #TEST nvarchar(max)
SET #TEST = N'SELECT * FROM OPENQUERY(x,'+CHAR(39)+'SELECT * FROM SALE WHERE OBJID IN '+#sql+CHAR(39)+')'
EXEC sp_executesql #TEST, N'#test1 datetime', #start
error:
LE DB provider "MSDASQL" for linked server "x" returned message "[ODBC Firebird Driver][Firebird]Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 77
#".
I also tried the non-parametrized approach, but couldn't figure out what formats to put the datetime in.

insert statement error On linked server (POSTGRESQL)

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!

DB2 printing logs

I am using DB2 IBM data studio, and I cannot print logs:
create procedure test_ticket
begin
declare stmt varchar(500);
DECLARE QTY INTEGER;
SET QTY = (select count(*) from test.ticket);
CALL DBMS_OUTPUT.PUT_LINE( QTY );
end
No authorized routine named "DBMS_OUTPUT.PUT_LINE" of type "PROCEDURE"
having compatible arguments was found.. SQLCODE=-440, SQLSTATE=42884,
DRIVER=4.18.60
As #mustaccio surmised, the DBMS_OUTPUT module was not present in DB2 9.5. It was introduced as part of the Oracle compatibility features in DB2 9.7.
You may also want to note that DBMS_OUTPUT.PUT_LINE takes an argument of VARCHAR, not INT.

Postgres SQL operator does not exist; character = integer;

I am do replicating of two database (SQL Server 2000 and PostgreSQL). I use http://blog.hagander.net/archives/103-Replicating-from-MS-SQL-Server-to-PostgreSQL.html for this. Then I do last step the
ERROR: operator does not exist: character = integer; Error executing the query
appeared. I use the PostgreSQL 8.4.6 for that and ODBC drivers (all psqlodbc_08_04_0100.zip, psqlodbc_08_04_0200.zip) from here i also try to delete and install version that Synaptic called 9.0.2-1 and update odbc drivers i try (psqlodbc_09_00_0100.zip, psqlodbc_09_00_0101.zip, psqlodbc_09_00_0200.zip) it also return that error. The query launched from delphi where i use only System DSN runs normally
You need to fix your SQL statement.
I bet you have something like
WHERE character_column = 1
and you need to change that to
WHERE character_column = '1'
use single quote 'your_value' for non numeric data and double quote for column name and never the opposite.
select status, sum(amount) as sum from "sales" where ("date" <= '2017-04-30 23:59:59' and "customer_id" = 1) and "sales"."deleted_at" is null group by "status"

DB2 error on TSQL using SELECT AS alias and an inner join

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?