0

I'd like to compare 2 queries results in order to get non corresponding data.

I have a MS SQL server that is linked to a MySQL server.

My first query get the emails and the number associated to it on MS SQL database:

select email, number  from [DB].[dbo].[Contacts];

My second query get the emails and the number associated to it on MySQL database:

select email, number from database.contacts;

I know that I can run a query on MySQL from MS SQL by using this kind of query:

SET @SQLStr = 'SELECT * FROM OPENQUERY([DB-01],''select email, number from database.contacts'')'
EXEC(@SQLStr)

But I have no idea of how to read and compare the results.

I'd like to know if email and number association existing in MS SQL database is different in MySQL server.

i.e.: if email = '[email protected]' and number = 00222 on MS SQL but email '[email protected] and number = 012325 on MySQL, I want to get the second result that is different.

2
  • 2
    What comparison do you want to do, and what is the expected output, if any? Commented Feb 27, 2019 at 10:15
  • I have edited the question to add the expected result Commented Feb 27, 2019 at 10:22

1 Answer 1

1

Why using dynamic SQL when there's nothig dynamic about it?

You can use standard query:

SELECT * FROM OPENQUERY([DB-01],'select email, number from database.contacts')

With your SQL Server query you'll get two resultsets.

To compare results you could use EXCEPT, to get records from one table not existing in another table.

Other way is to use OUTER JOIN~and see if any records haveNULL`s meaning that there is no match in the other table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.