function TDataSet.CopyFields(Source: TDataSet): Integer;
// copies matching fields in current records- returns number of fields copied
var
FieldCtr: Integer;
DestField, SourceField: TField;
begin
Result := 0;
for FieldCtr := 0 to Source.FieldCount - 1 do begin
SourceField := Source.Fields[FieldCtr];
Field := FindField(SourceField.FieldName);
if not Assigned(Field) then Continue;
if Field.ClassType = TDataSetField then begin // nested datasets
while TDataSetField(Field).NestedDataSet.RecordCount > 0 do
TDataSetField(Field).NestedDataSet.Delete;
TDataSetField(SourceField).NestedDataSet.First;
while not TDataSetField(SourceField).NestedDataSet.Eof do begin
TDataSetField(Field).NestedDataSet.Append;
CopyFields(TDataSetField(Field).NestedDataSet, TDataSetField(SourceField).NestedDataSet);
TDataSetField(Field).NestedDataSet.Post;
TDataSetField(SourceField).NestedDataSet.Next;
end;
end else
Field.Value := SourceField.Value;
Inc(Result);
end;
end;
This would typically be used as follows:
SourceDS.First;
while not SourceDS.EOF do begin
if DestDS.Locate({info required to find matching record}) then begin
DestDS.Edit;
DestDS.CopyFields(SourceDS);
DestDS.Post;
end;
SourceDS.Next;
end;