Reputation: 20028
Lets say I have 2 datasets: A and B, that have the same columns. I want to get the 'distance path' - minimal operations needed to be done, to change datasets A to B.
I can assume, that A and B contain only in few differences and I'm looking for something better, then:
How can this be archived?
Upvotes: 0
Views: 438
Reputation: 76537
If you know how to use SQL:
SELECT a.* FROM a
LEFT JOIN b on (a.field1 = b.field1 AND a.field2 = b.field2 AND ....)
WHERE b.field1 IS NULL
Will give you all fields in A that are not in B.
Now do
INSERT INTO b
SELECT a.* FROM a
LEFT JOIN b on (a.field1 = b.field1 AND a.field2 = b.field2 AND ....)
WHERE b.field1 IS NULL
And then do (or don't do depending on your needs).
DELETE b FROM b
LEFT JOIN a ON (a.field1 = b.field1 and a.field2 = b.field2 AND ...)
a.field1 IS NULL
Now table a
and b
will be the same.
Delphi code like this should do the trick, but the exact code depends on your database and the query components used.
procedure TForm1.equalize(A, B: TDataset);
var
tablenameA: string;
tablenameB: string;
MyQuery: TQuery;
begin
tablenameA:= IProviderSupport(A).PSGetTableName;
tablenameB:= IProviderSupport(B).PSGetTableName;
MyQuery:= TQuery.Create(self);
MyQuery.Database:= .....
MyQuery.SQL.Text:= ' INSERT INTO '+tablenameA+' .....
MyQuery.ExecSQL;
end;
Upvotes: 3
Reputation: 84540
If you want to find out how to transform A into B, you need to know what the difference between A and B is. There's a very simple general algorithm for that, though it might be more complicated depending on how many fields are in your dataset and how many of them can be different from A to B. But here's the general idea:
less than
, equal
or greater than
.Once you're done, you'll have a full list of differences between A and B, which will make the steps needed to transform A to B simple to calculate.
Upvotes: 2