I have 2 tables with the same columns – T1, T2. I want to update table T1 columns from the columns that exist in T2 based on the key column: if the key column exists then update the rest of the columns of T1 from T2, if doesnt exist, then insert the whole row from T2 to T1.
This query doesnt do the job:
IF EXISTS (SELECT keyC FROM T2 WHERE keyC in (select keyC from T1))
UPDATE T1 SET T1.c1 = T2.c1,
T1.c2 = T2.c2,
from T2 WHERE T2.keyC in (select keyC from T1)
ELSE (INSERT INTO T1 select * from T2)
Any idea how to fix it?
Thanks in advance,
Greg
Since you use SQL Server 2005 you can’t use merge and have to do two statements. One update and one insert.