简体   繁体   中英

Query not returning same results local vs openquery

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.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM