Copy new records from datatable and identify changes in old records

Posted by Betite on Stack Overflow See other posts from Stack Overflow or by Betite
Published on 2012-10-09T09:27:15Z Indexed on 2012/10/09 9:37 UTC
Read the original article Hit count: 187

Assume there are two tables: Remote_table and My_table.

Remote_table has 6 columns:

**PROJECT   JOB_TYPE   MONTH  YEAR**       HOURS     IS_DELETED
134393     70         1       2013      30     0
134393     70         2       2013      50     0
134393     70         3       2013      80     0
134393     70         10      2012      10     0
134393     70         11      2012      0      0
134393     70         12      2012      15     0

My_table is a copy of remote_table.

I tried to copy only the new records from the remote_table by this query:

SELECT * FROM [remote_DB].[LudanProjectManager].[dbo].Remote_table
EXCEPT
SELECT * FROM My_table

It works OK but I get a duplicate primary key exception when changes have been made on the remote_table on the hours column.

Can anyone think of a way to copy only the new records from remote_table and if changes has been made on old records, to identify them and update the my_table to correspond?

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about remote-server