本地查询与 openquery 未返回相同结果

发布于 2025-01-11 15:33:33 字数 892 浏览 0 评论 0原文

我有一个链接到 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 执行相同的查询,我会在对应于的列上得到空值

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 中看到这些值但在 SQL Server 中看不到这些值。

I have an Oracle database linked to a SQL Server database.

Whenever I perform a specific query in Oracle I get expected data.

The fields I'm selecting are

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

If I do the same query with openquery I get null values on the column which corresponds to

CASE WHEN J.SOURCE='MUST' THEN SRVCD.DACRENR ELSE W.DEBRIEFDATE END as "DEBRIEFDATE"

In this case the column holds dates and the ones that correspond to SRVCD.DACRENR are being shown and the W.DEBRIEFDATE dates are not.

I don't know why I'm seeing those values correctly in Oracle but not in the openquery result. I tried changing the values with TO_CHAR and TO_DATE and all scecnarios I see the values in Oracle but not in SQL Server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2025-01-18 15:33:33

您可以尝试将日期转换为 varchar 以便它与相同的数据类型匹配吗?
或者发布一些示例数据

Convert(varchar(10), your field,105)

Could you try a convert to varchar on your date so it match the same datatype?
Or post some sample data

Convert(varchar(10), your field,105)
萌梦深 2025-01-18 15:33:33

不确定如何在 OPENQUERY 中声明变量。有一种直接方式,您不使用任何变量来存储查询以供以后执行,还有一种间接方式,与直接方式相反。大多数时候,间接方式是获得预期结果的最佳方式。

首先,您需要声明一些变量并使用 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/

希望这有帮助。

Not sure how you declare your variables in OPENQUERY. There is one direct way where you don't use any variables to store the query for latter execution, and indirect way which is the opposite way of direct way. Most of the times, indirect way is the best way in order to get the desired results.

First you will need to declare some variables and execute it using EXEC() function. Let's see how it is done.

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

Points to remember:

  • Everytime you filter something based on a value, it should always be inside double quotes ('').
  • SQL dates and ORACLE date formats are different. You have to convert it to varchar first.
  • Always be careful for the quotes that you are adding. Even one single quote can mess the entire OPENQUERY query.
  • The CONVERT() above has YOUR_PREFERRED_DATE_FORMAT which means you have to set the preferred date format according to your requirement. Here is the complete reference just in case if you get stuck: https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文