Board index » delphi » How to Copy a Record between Identical Tables

How to Copy a Record between Identical Tables

Can anyone tell me the preferred way of copying a record between two tables
having identical structures.

The use of a batchmove component seems a bit extreme for one record.

Anthony Magauran

 

Re:How to Copy a Record between Identical Tables


One approach would be to copy the record into a variant array  and then copy
the contents of the variant array to the other table.

Another approach would be to iterate the fields of the first table and
assign the field contents to the second table on a field by field basis.

Hope this helps,

Wes

Re:How to Copy a Record between Identical Tables


A better approach would be to specify the field names.  That way the two
tables don'y require field synchronization.

Arrow

Quote
Wes Hill wrote in message <7cbocl$o...@forums.borland.com>...
>One approach would be to copy the record into a variant array  and then
copy
>the contents of the variant array to the other table.

>Another approach would be to iterate the fields of the first table and
>assign the field contents to the second table on a field by field basis.

>Hope this helps,

>Wes

Re:How to Copy a Record between Identical Tables


On Fri, 12 Mar 1999 12:52:38 -0000, "Anthony Magauran"

Quote
<magau...@btinternet.com> wrote:
>Can anyone tell me the preferred way of copying a record between two tables
>having identical structures.

>The use of a batchmove component seems a bit extreme for one record.

In addition to Wes Hill's two suggestions...

You could use an INSERT statement executed from a TQuery. The source table
would be used in the statement by way of a SELECT subquery. Since the
structures of the two tables are identical, no need to list individual
fields in either the INSERT or SELECT statements. Use a WHERE clause in the
SELECT subquery to uniquely identify the one source record to copy to the
other table, passing the unique value(s) to the statement as a parameter.

Using the sample Paradox table CUSTOMER.DB for example (the unique field is
CustNo), the statement might look like this:

  INSERT INTO Customer1
  SELECT *
  FROM Customer2
  WHERE CustNo := CustNoParam

//////////////////////////////////////////////////////////////////////////
Steve Koterski                    "My problem lies in reconciling my gross
Technical Publications            habits with my net income."
INPRISE Corporation                             -- Errol Flynn (1909-1959)
http://www.borland.com/delphi

Re:How to Copy a Record between Identical Tables


procedure dgCopyRecordByName(Source: TDataSet;
                       Destination: TTable);
{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.

Quote
}

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; //for
end;

Bill

--

Bill Todd - TeamB
(TeamB cannot respond to email questions. To contact me
 for any other reason remove nospam from my address.)

Re:How to Copy a Record between Identical Tables


On Fri, 12 Mar 1999 12:52:38 -0000, "Anthony Magauran"

Quote
<magau...@btinternet.com> wrote:
>Can anyone tell me the preferred way of copying a record between two tables
>having identical structures.

Anthony,

Supposing you have no persistent field definitions on either TTable
component and both tables have exact identical structures:

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

HTH,

Jan

Re:How to Copy a Record between Identical Tables


Anythony:  check out table.batchmove   mIKE

Other Threads