我有一个链接到SQL Server数据库的Oracle数据库。
每当我在Oracle中执行特定查询时,我都会得到预期的数据。
我选择的字段是
SELECT
J.JOBNUMBER AS JOBNUMBER,
J.OPENDATE AS OPENDATE,
S.SERVICEREPORTNUMBER AS SERVICEREPORTNUMBER,
F.SSO AS SSO,
S.DATEOFVISIT AS DATEOFVISIT,
CASE WHEN J.SOURCE='MUST' THEN SRVCD.DACRENR ELSE W.DEBRIEFDATE END as "DEBRIEFDATE",
J.CLOSEDDATE AS CLOSEDDATE,
INITCAP(HR.COUNTRY_NAME) as "COUNTRY",
F.NAME AS NAME,
SY.MODALITY AS MODALITY,
HR.MANAGER_SSOID AS MANAGER_SSOID,
HR.MANAGER_NAME AS MANAGER_NAME
如果我用openquery进行相同的查询,我会在与对应的列上得到null值
CASE WHEN J.SOURCE='MUST' THEN SRVCD.DACRENR ELSE W.DEBRIEFDATE END as "DEBRIEFDATE"
在这种情况下,该列包含日期,并且显示了与SRVCD.DACRENR相对应的日期,而没有显示W.DEBRIEFDATE日期。
我不知道为什么我在Oracle中正确地看到了这些值,但在openquery结果中却没有。我试着用TO_CHAR和TO_DATE以及我在Oracle中看到的所有scecnarios来更改值,但在SQL Server中没有。
你能在约会时尝试转换为varchar,使其匹配相同的数据类型吗?或者张贴一些样本数据
Convert(varchar(10), your field,105)
首先,您需要声明一些变量,并使用EXEC()
函数执行它。让我们看看是怎么做的。
DECLARE @ORACLE_ENV nvarchar(20) = 'YOUR_ORACLE_DB_INSTANCE_HERE'
DECLARE @ORACLE_SQL nvarchar(max)
SET @ORACLE_SQL = 'SELECT * FROM OPENQUERY ('+@ORACLE_ENV+',''' +SELECT
J.JOBNUMBER AS JOBNUMBER,
J.OPENDATE AS OPENDATE,
S.SERVICEREPORTNUMBER AS SERVICEREPORTNUMBER,
F.SSO AS SSO,
S.DATEOFVISIT AS DATEOFVISIT,
CASE WHEN J.SOURCE=''MUST'' THEN CONVERT(VARCHAR(15), SRVCD.DACRENR, YOUR_PREFERRED_DATE_FORMAT) ELSE CONVERT(VARCHAR(15), W.DEBRIEFDATE, YOUR_PREFERRED_DATE_FORMAT) END as DEBRIEFDATE,
J.CLOSEDDATE AS CLOSEDDATE,
INITCAP(HR.COUNTRY_NAME) as COUNTRY,
F.NAME AS NAME,
SY.MODALITY AS MODALITY,
HR.MANAGER_SSOID AS MANAGER_SSOID,
HR.MANAGER_NAME AS MANAGER_NAME + ''')'
EXEC sp_executesql @ORACLE_SQL
需要记住的要点:
- 每次根据值筛选内容时,它都应该位于双引号(''(内
- SQL日期和ORACLE日期格式不同。您必须先将其转换为varchar
- 始终要小心添加的引号。即使是一个报价也可能扰乱整个OPENQUERY查询
- 上面的CONVERT((具有YOUR_PREFERRED_DATE_FORMAT,这意味着您必须根据自己的要求设置首选日期格式。以下是完整的参考资料,以防您陷入困境:https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
希望这能有所帮助。