WHERE clause in OPEN QUERY

会有一股神秘感。 提交于 2019-12-24 18:56:37

问题


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:

  1. DAT_CLOSEDATE must be dynamic fetching the yesterday data (today-1)

  2. The DAT_CLOSEDATE column data type in Oracle is FLOAT and DATETIME 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!