Question

I have two tables both named employer. One table resides on a MSSQL the other on a MySQL. The MySQL DB has been Linked.

I'm trying to update all rows in the MSSQL DB table with the data from the MySQL DB where the data is different.

Ideally, because there's so many rows, I would prefer to only UPDATE the rows that have different data.

Each DB Table has 2 Columns idemployees and employeescol1.

Below is a working sample of a script that will update ALL the rows from the Linked MySQL DB:

UPDATE MSSQLDB.dbo.employer
SET employeescol1 = emp2.employeescol1  
FROM OPENQUERY(LinkedServer, 'SELECT * FROM employees') as emp2  
WHERE
   MSSQLDB.dbo.employer.idemployees = emp2.idemployee

How do I add a WHERE clause to say something like WHERE employeescol >= (insert variable)???

Was it helpful?

Solution

Something like this should work. You can also use synonyms, if the .... get a bit much:

;with cte
as
(SELECT
    *
from
   OPENQUERY(LinkedServer, 'SELECT * FROM employees')
)
UPDATE MSSQLDB.dbo.employer
SET 
    employer.employeescol1 = cte.employeescol1  
FROM
    MSSQLDB.dbo.employer 
inner join
    cte
on
    cte.idemployees = employer.idemployee
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top