-- 1. remove rows in T2 that are not in T1 delete T2 where not exists ( select 1 from T1 where T1.Ka = T2.Ka and T1.Kb = T2.Kb and ... -- The where clause can be generated by something like: -- join(' and ', map { "T1.$_ = T2.$_" } @key_col_names) ) -- 2. synchronize rows that exist in both update T1 set T1.Va = T2.Va, T1.Vb = T2.Vb, ... from T1, T2 where T1.Ka = T2.Ka and T1.Kb = T2.Kb and ... and ((T1.Va != T2.Va) or (T1.Vb != T2.Vb) ...) -- Check that your platform supports a "from" clause -- in update statements. -- You can actually omit the "value" test; it just eliminates -- unnecessary data movement. Beware that nullable columns -- need more complicated tests. -- 3. insert new rows insert T2 (Ka, Kb, ..., Va, Vb, ...) select Ka, Kb, ..., Va, Vb, ... from T1 where not exists ( select 1 from T2 where T1.Ka = T2.Ka and T1.Kb = T2.Kb and ... ) #### 1. Pull one row from each result set. LOOP: 2. Compare T1 keys to T2 keys 3. If T1 is "larger" Generate a "delete" for T2 keys "Advance" T2, return to loop 4. If T1 is "smaller" Generate an "insert" for T1 keys and values "Advance" T1, return to loop 5. Compare T1 and T2 values 6. If different, generate an "update" statement 7. "Advance" both T1 and T2, return to loop