Copy record in the same table

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Thomas J.
Posts: 95
Joined: Mon 21 Nov 2005 12:16
Location: Germany

Copy record in the same table

Post by Thomas J. » Tue 14 Feb 2006 10:49

Hello, is it possible to make a copy of a record from table a and paste this record to the same table?
Or is the only solution by iterating over .Fields.Fields[]?

I thought something like this
TMyTable* table = ......;
// locate the record which has to be copied
table->LocateRecord(...);
// make a copy of the located record
table->CopyRecord();
// change few fields
table->FieldByName("d_created") = now();

table->Post();

Thanks for your help

GEswin
Posts: 186
Joined: Wed 03 Nov 2004 16:57
Location: Spain
Contact:

Post by GEswin » Tue 14 Feb 2006 15:48

You can always use the Insert stament using a query/cmd..

Code: Select all

  INSERT INTO table (fields...) select fields.. from table where field = xxx

Thomas J.
Posts: 95
Joined: Mon 21 Nov 2005 12:16
Location: Germany

Post by Thomas J. » Tue 21 Feb 2006 08:45

This is not working due to the problem of PK.
I want to copy a record in the same table with exception of the PK. And if I execute the statement like you posted I get the error.
This is what I tried also and there is also exits another syntax, but this is not working too.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Tue 21 Feb 2006 13:37

Try the same but don't forget to define your primary key as AUTO_INCREMENT and exclude exclude its field from list of fields you use in SELECT and INSERT statements.
Another way is copying field by field in a loop using MyTable or MyQuery.

Post Reply