2007. augusztus 25., szombat

How to copy a record from one table to another


Problem/Question/Abstract:

Assuming I have Table1 and Table2, that have identical structures (same fields), how can I transfer all of the fields in the current record to a new record in the second table? For example, I want record no. 3 in Table1 to be appended to Table2.

Answer:

Solve 1:

for I := 0 to Table1.FieldCount do
  Table2.Fields[I].AsVariant := Table1.Fields[I].AsVariant;


Solve 2:

Copies a record from a DataSet to a Table by field names. This is much safer than using the Fields property since the order of fields in the Fields array depends on the order of instantiation of the TField objects, not the order of fields in the table. This procedure assumes that the corresponding fields in the source and destination datasets have the same names.

Parameters:
Source = The source dataset
Destination = The destination table

procedure dgCopyRecordByName(Source: TDataSet; Destination: TTable);
var
  LastField, L: Integer;
begin
  Destination.Edit;
  LastField := Source.FieldCount - 1;
  for L := 0 to LastField do
  begin
    {Skip fields that do not exist in the destination table}
    if Destination.FieldDefs.IndexOf(Source.FieldDefs[L].Name) < 0 then
      Continue;
    {Skip fields that are read only in the destination dataset}
    if Destination.FieldByName(Source.FieldDefs[L].Name).ReadOnly then
      Continue;
    {Copy the field}
    Destination.FieldByName(Source.FieldDefs[L].Name).Assign
      (Source.FieldByName(Source.FieldDefs[L].Name));
  end;
end;


Solve 3:

var
  iCount: LongInt;
  sName: string;
begin
  Table2.Insert;
  for iCount := 0 to Table2.FieldCount - 1 do
  begin
    sName := Table2.Fields[iCount].FieldName;
    if (Table1.FindField(sName) <> nil) and (sName <> 'ID') then
      Table2.FieldByName(sName).Assign(Table1.FieldByName(sName));
  end;
  Table2.Post;
end;

If you work with FieldByName, there are 2 advantages: You can copy only the fields you want. In the upper example, the field "ID" would not be copied. The construction of the two tables must not the same. Only fields with the same name would be copied. If you are sure, the construction is the same

for iCount := 0 to Table2.FieldCount - 1 do
  Table2.Fields[iCount].Assign(Table1.Fields[iCount]);

also works.


Solve 4:

procedure CopyRecord(Tabelle: TTable);
var
  feldwert: Variant;
  i: Word;
begin
  with DataModule1 do
  begin
    feldwert := VarArrayCreate([0, Tabelle.FieldCount - 1], varVariant);
    for i := 0 to Tabelle.FieldCount - 1 do
      feldwert[i] := Tabelle.Fields[i].Value;
    Tabelle.Append;
    for i := 0 to Tabelle.FieldCount - 1 do
      Tabelle.Fields[i].Value := feldwert[i];
  end;
end;


Solve 5:

procedure AppendCurrent(Dataset: TDataset);
var
  aField: Variant;
  i: Integer;
begin
  {Create a variant Array}
  aField := VarArrayCreate([0, DataSet.Fieldcount - 1], VarVariant);
  {Read values into the array}
  for i := 0 to (DataSet.Fieldcount - 1) do
  begin
    aField[i] := DataSet.fields[i].Value;
  end;
  DataSet.Append;
  {Put array values into new the record}
  for i := 0 to (DataSet.Fieldcount - 1) do
  begin
    DataSet.fields[i].Value := aField[i];
  end;
end;


Solve 6:

The following is a chunk of code that I use to copy TTable rows. This function assumes that you are handling the insert and post calls yourself.

function CopyRow(Source, Dest: TTable): Boolean;
var
  n: Integer;
begin
  Result := False;
  for n := 0 to Source.FieldCount - 1 do
  begin
    try
      Dest.Fields[n].Assign(Source.Fields[n]);
    except
      Exit;
    end;
  end;
  Result := True;
end;

Nincsenek megjegyzések:

Megjegyzés küldése