sql server linked server to oracle returns no data found when data exists

半腔热情 提交于 2019-11-29 13:58:48
irieill

Today I experienced the same problem with an inner Join. As creating a Table Valued Function suggested by codechurn or using a Temporary Table suggested by user1935511 or changing the Join Types suggested by cymorg are no options for me, I like to share my solution.

I used Join Hints to drive the query optimizer into the right direction, as the problem seems to rise up from nested loops join strategy with the remote table locally . For me HASH, MERGE and REMOTE join hints worked.

For you REMOTE will not be an option because it can be used only for inner join operations. So using something like the following should work.

select *
from eopf.Batch b
join eopf.BatchFile bf
  on b.BatchID = bf.BatchID
left outer merge join [OM_ORACLE]..[OM].[DOCUMENT_UPLOAD] du
  on bf.ReferenceID = du.documentUploadID;
user1935511

I've had the same problem. Solution1: load the data from the Oracle database into a temp table, then join to that temp table instead - here's a link.

From this post a link you can find out that the problem can be with using left join. I've checked with my problem and after changing my query it solved the problem.

In my case I had a complex view made from a linked table, 3 views based on the linked table and a local table. I was using Inner Joins throughout and this problem manifested. Changing the joins to Left and Right Outer Joins (as appropriate) resolved the issue.

I got the same problem and resolved it by avoiding the '=' operator try using

select * from [OM_ORACLE]..[OM].[DOCUMENT_UPLOAD] where documentUploadID < 0

Instead.

Thanks

Another way to work around the problem is to pull back the Oracle data into a Table Valued Function. This will cause SQL Server to go out and retrieve all of the data from Oracle and throw it into a resultant table variable. For all intent and purpose, the Oracle data is now "local" to SQL Server if you use the resultant Table Valued Function in a query.

I believe the original problem is that SQL Server is trying to optimize the execution of your compound query which includes the remote Oracle query results in-line. By using a Table Valued Function to wrap the Oracle call, SQL Server will optimize the compound query on the resultant table variable returned from the function and not the results from the remote query execution.

CREATE function [dbo].[documents]()
returns @results TABLE (

    DOCUMENT_ID INT NOT NULL,
    TITLE VARCHAR(6) NOT NULL,
    LEGALNAME VARCHAR(50) NOT NULL,
    AUTHOR_ID INT NOT NULL,
    DOCUMENT_TYPE VARCHAR(1) NOT NULL,
    LAST_UPDATE DATETIME
) AS 

BEGIN
INSERT INTO @results
SELECT     CAST(DOCUMENT_ID AS INT) AS DOCUMENT_ID, TITLE, LEGALNAME, CAST(AUTHOR_ID AS INT) AS AUTHOR_ID, DOCUMENT_TYPE, LAST_UPDATE
FROM         OPENQUERY(ORACLE_SERVER, 
                      'select DOCUMENT_ID, TITLE, LEGALNAME, AUTHOR_ID, DOCUMENT_TYPE, FUNDTYPE, LAST_UPDATE
                       from documents')

return
END

You can then use the Table Valued Function as it it were a table in your SQL queries:

SELECT * FROM DOCUMENTS()

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