sql server - compare 2 tables if match update if not match insert -
i have 2 tables same table structure. table having transaction 3 unique key in each record. table b have condition base record only.
i want compare both tables if table b has matching record want update , table b have not matching record insert in table b.
can please suggest best way ssis or thing else
the easiest way merge
statement:
merge table_b using table_a on tablea.id1 = table_b.id1 , tablea.id2 = table_b.id2 , tablea.id3 = table_b.id3 when matched update set = table_a.a, b = table_a.b -- etcetera... when not matched insert (a, b) values (table_a.a, table_a.b) -- etcetera... when not matched source delete -- if necessary... ;
by way, don't forget ";" @ end. sql server doesn't need them, merge
does.
Comments
Post a Comment