-3

I want to move data from table1 to table2 (both the tables have same structure)with condition of comparing their emaild (unquie)in both the tables...if the data of emaild exits in table 2 I should not load that data otherwise I have to insert the record in table 2..these two tables are in two different databases of same sql server instance... Could you please provide me with the syntax.. Thanks in advance

  • Does this answer your question? [SQL - Insert Where Not Exists](https://stackoverflow.com/questions/46779223/sql-insert-where-not-exists) – Charlieface Oct 29 '21 at 12:25

1 Answers1

2
INSERT db2.dbo.table2(key, other cols)
  SELECT key, other cols
  FROM db1.dbo.table1 AS t1
  WHERE NOT EXISTS 
  (
    SELECT 1 
      FROM db2.dbo.table2 AS t2
      WHERE t2.key = t1.key
  );

You can double-check you're getting the right rows by commenting out the first line.

Here's an example with a procedure on db<>fiddle.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490