问题
I have created a procedure in SQL Server 2012 for the purpose of fetching the yesterday data from an Oracle table and inserting it into a SQL Server table using OPENQUERY like this:
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT
LEAD(CONVERT(VARCHAR, CONVERT(DATETIME, '01-JAN-1970 03:00:00', 120) + [DAT_CLOSEDATE] / (24 * 60 * 60), 120), 1, CONVERT(VARCHAR, CONVERT(DATETIME, '01-JAN-1970 03:00:00', 120) + [DAT_CLOSEDATE] / (24 * 60 * 60), 120)) OVER (PARTITION BY [TXT_TICKETNUMBER] ORDER BY [DAT_CLOSEDATE]) AS [CLOSE_DATE]
INTO
#Temp
FROM
OPENQUERY(ORACLE_DB, 'SELECT DAT_CLOSEDATE, TXT_TICKETNUMBER FROM SCHEME.TABLE')
WHERE
[DAT_CLOSEDATE] = DATEADD(d, -1, GETDATE())
Everything is working as expected, but the problem in the SQL Server syntax is within the WHERE
clause, so that I want to make it inside the OPENQUERY
considering the following:
DAT_CLOSEDATE
must be dynamic fetching the yesterday data (today-1)The
DAT_CLOSEDATE
column data type in Oracle isFLOAT
andDATETIME
in SQL Server
I expect OPENQUERY
syntax to be something like the following:
OPENQUERY(ORACLE_DB, 'SELECT DAT_CLOSEDATE, TXT_TICKETNUMBER
FROM SCHEME.TABLE
WHERE [DAT_CLOSEDATE] = DATEADD(d, -1, GETDATE())')
回答1:
If looking for an equivalent on Oracle then:
where DAT_CLOSEDATE = sysdate-1
or
where DAT_CLOSEDATE = trunc(sysdate)-1
The second version sets the time of day to 00:00:00
回答2:
Suppose DAT_CLOSEDATE
column has float type values such as 20181231202534
, 20181231202713
... that is, they're of type yyyymmddhh24miss
converted from a date value, where yyyy
stands for year
, mm
for month, dd
for day, hh24
for hour in the range of 00-23
, mi
for minute, and ss
is for second.
In this case, the following block might be invoked :
OPENQUERY(ORACLE_DB, 'SELECT DAT_CLOSEDATE, TXT_TICKETNUMBER
FROM SCHEMA.MYTABLE
WHERE substr(DAT_CLOSEDATE,1,8) = to_char(sysdate-1,''yyyymmdd'')')
to get the values of the previous day.
来源:https://stackoverflow.com/questions/53993938/where-clause-in-open-query