在 TO_DATE 函数中使用变量

Using a variable in TO_DATE function

我正尝试在 where 子句中使用今天的日期通过 LINKED SERVER return 来自 ORACLE 的数据

代码:

DECLARE @var_date char(10)
SET @var_date = (SELECT CONVERT(DATE,GETDATE()))

SELECT TOP(25) * FROM OPENQUERY (OPICS, 'SELECT 
CASE 
  WHEN OPICS.FXDH.BR = 10 THEN ''Botswana''
  WHEN OPICS.FXDH.BR = 20 THEN ''Mozanmbique''
  WHEN OPICS.FXDH.BR = 30 THEN ''Rwanada''
  WHEN OPICS.FXDH.BR = 40 THEN ''Tanzania''
  WHEN OPICS.FXDH.BR = 50 THEN ''Zambia''
  WHEN OPICS.FXDH.BR = 60 THEN ''Zimbabwe''
END AS COUNTRY,
OPICS.FXDH.DEALNO,
to_char (OPICS.FXDH.DEALDATE,''YYYY-MM-DD'') as Deal_Date,
to_char (OPICS.FXDH.VDATE, ''YYYY-MM-DD'') as V_Date,
OPICS.FXDH.PS, 
OPICS.FXDH.CCY, 
OPICS.FXDH.CCYAMT, 
OPICS.FXDH.CCYRATE_8,
OPICS.FXDH.CTRCCY, 
to_char (OPICS.OPER.OPERNAME) as OPERNAME,
to_char (OPICS.CUST.CFN1) as CFN1,
to_char (OPICS.PROD.PDESC) as PDESC,
to_char (OPICS.PORT.PORTDESC) as Report_Description
FROM OPICS.FXDH FXDH
INNER JOIN OPICS.OPER OPER ON (FXDH.BR = OPER.BR) AND (FXDH.TRAD = OPER.OPER)
INNER JOIN OPICS.CUST CUST ON (FXDH.CUST = CUST.CNO)
INNER JOIN OPICS.PROD PROD ON (FXDH.PRODCODE = PROD.PCODE)
INNER JOIN OPICS.PORT PORT ON (FXDH.PORT = PORT.PORTFOLIO) AND (FXDH.BR = PORT.BR)
 WHERE OPICS.FXDH.DEALDATE = TO_DATE(''2018-08-29'', ''YYYY-MM-DD'')  
    --dateString
AND OPICS.FXDH.BR = 10
--ORDER BY OPICS.FXDH.DEALDATE DESC');

以上代码工作正常并且 returns 值

有没有办法在我 need/want.

的日期使用我的变量 @var_date 而不是硬拷贝

我尝试使用以下代码但在使用代码时出现错误:

  1. WHERE OPICS.FXDH.DEALDATE = TO_DATE(@var_date, ''YYYY-MM-DD'')
  2. WHERE OPICS.FXDH.DEALDATE = CONVERT(DATETIME , @var_date, 120)

正如我在评论中提到的 您需要使用动态 sql。 OPENQUERY 只接受文字字符串。

因此,您需要执行以下操作:

DECLARE @var_date date = GETDATE(); --Note the datatype change

DECLARE @SQL nvarchar(MAX);
SET @SQL = N'SELECT TOP(25) * FROM OPENQUERY (OPICS, ''SELECT CASE
            WHEN OPICS.FXDH.BR = 10 THEN ''Botswana''
            WHEN OPICS.FXDH.BR = 20 THEN ''Mozanmbique''
            WHEN OPICS.FXDH.BR = 30 THEN ''Rwanada''
            WHEN OPICS.FXDH.BR = 40 THEN ''Tanzania''
            WHEN OPICS.FXDH.BR = 50 THEN ''Zambia''
            WHEN OPICS.FXDH.BR = 60 THEN ''Zimbabwe''
       END AS COUNTRY,
       OPICS.FXDH.DEALNO,
       to_char(OPICS.FXDH.DEALDATE, ''YYYY-MM-DD'') AS Deal_Date,
       to_char(OPICS.FXDH.VDATE, ''YYYY-MM-DD'') AS V_Date,
       OPICS.FXDH.PS,
       OPICS.FXDH.CCY,
       OPICS.FXDH.CCYAMT,
       OPICS.FXDH.CCYRATE_8,
       OPICS.FXDH.CTRCCY,
       to_char(OPICS.OPER.OPERNAME) AS OPERNAME,
       to_char(OPICS.CUST.CFN1) AS CFN1,
       to_char(OPICS.PROD.PDESC) AS PDESC,
       to_char(OPICS.PORT.PORTDESC) AS Report_Description
FROM OPICS.FXDH AS FXDH
     INNER JOIN OPICS.OPER AS OPER ON (FXDH.BR = OPER.BR)
                                  AND (FXDH.TRAD = OPER.OPER)
     INNER JOIN OPICS.CUST AS CUST ON (FXDH.CUST = CUST.CNO)
     INNER JOIN OPICS.PROD AS PROD ON (FXDH.PRODCODE = PROD.PCODE)
     INNER JOIN OPICS.PORT AS PORT ON (FXDH.PORT = PORT.PORTFOLIO)
                                  AND (FXDH.BR = PORT.BR)
WHERE OPICS.FXDH.DEALDATE = ' + QUOTENAME(CONVERT(DATETIME , @var_date, 112),'''') + N'
  --dateString
  AND OPICS.FXDH.BR = 10;
--ORDER BY OPICS.FXDH.DEALDATE DESC'');';

--PRINT @SQL; --Your debugging best friend
EXEC sp_executesql @SQL;

请注意,我还没有对此进行测试,但我假设 Oracle 接受日期字符串文字的 ISO 格式。如果不是,您可能需要更改样式类型或在 OPENQUERY 中应用一些逻辑以将其更改为 Oracle 可接受的类型。