Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Datatables & Merge Changes

Tags:

c#

datatable

I've got two datatables (A, B) with the same structure. I need to compare each row of A with that of B and the resultant Datatable C should have the row in A and the changes of that row in B below it. For rows which are identical (same values in A & B) the resulting Datatable shouldn't have these rows.

So the resulting Datatable should have each row in A and its non-identical row in B under it. Resulting table shouldn't have identical rows.

Can anyone please help me with C# code.

like image 506
vinodreddymk Avatar asked Nov 06 '22 17:11

vinodreddymk


1 Answers

the easiest way is a cross-database union:

create table merged
(select * from db1.t) union (select * from db2.t)

only unique rows are returned. to compare records, select rows that share the same key (the key column in merged will be non-unique).

select * from merged order by key

orders the results in the order you wanted.

select * from merged where key in
(select key from merged group by key having count(*) > 1)
order by key

will return only mismatched rows.

like image 178
jspcal Avatar answered Nov 15 '22 05:11

jspcal