Pergunta

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.

Foi útil?

Solução

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

Outras dicas

-- 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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top