Question

Below statement will return values from OpenQuery(MYSQL).

Select * From OpenQuery(MYSQL, 'Select * From ResearchRpt') a
Where Not Exists (Select * From dbo.ResearchRpt where Id_report = a.Id_report)

I want to reversed it where the result values should be came dbo.ResearchRpt

Thank.

Was it helpful?

Solution

Your query uses an anti-join. An anti-join can be implemented with LEFT JOIN + WHERE IS NULL just as well as with NOT EXISTS. So, convert your script to the LEFT JOIN version, swap the sides, and you are done:

SELECT ms.*
FROM dbo.ResearchRpt ms
  LEFT JOIN OPENQUERY(MYSQL, 'SELECT * FROM ResearchRpt') my
    ON my.Id_report = ms.Id_report
WHERE my.Id_report IS NULL

OTHER TIPS

-- Only exists

SELECT ms.*, my.Id_report
FROM dbo.ResearchRpt ms
LEFT JOIN OPENQUERY(MYSQL, 'SELECT * FROM ResearchRpt') my
ON my.Id_report = ms.Id_report
WHERE my.Id_report IS NOT NULL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top