views:

539

answers:

3

I´m trying to copy data from one master table and 2 more child tables. When I select one record in the master table I copy all the fields from that table for the other. (Table1 copy from ADOQuery the selected record)

procedure TForm1.copyButton7Click(Sender: TObject);
SQL.Clear;
SQL.Add('SELECT * from ADoquery');
SQL.Add('Where numeracao LIKE ''%'+NInterv.text);// locate record selected in Table1 NInterv.text)
Open;

 // iniciate copy of record´s

begin
   while not tableADoquery.Eof do

      begin
         Table1.Last;
         Table1.Append;// how to append if necessary!!!!!!!!!!
         Table1.Edit;
         Table1.FieldByName('C').Value := ADoquery.FieldByName('C').Value;
         Table1.FieldByName('client').Value := ADoquery.FieldByName('client').Value;
         Table1.FieldByName('Cnpj_cpf').Value := ADoquery.FieldByName('Cnpj_cpf').Value;
         table1.Post; 
         table2.next;///
       end;
end;

//How can i update the TableChield,TableChield1 from TableChield_1 and TableChield_2 fields at the same time?

do the same for the child tables TableChield <= TableChield_1
TableChield1 <= TableChield_2

thanks

A: 

Question is not understandable. Please take time to redact it in a way that helps the reader

Stephane
+1  A: 

The fields will all be updated at the same time. The actual update is performed when you call post (or not even then, it depends if the Batch Updates are on or off).

But please reconsider your logic. It would be far more efficient to use SQL statements (INSERT) in order to insert the data to the other table

SQL.Clear;
SQL.Add('INSERT INOT TABLE_1(C, client, Cnpj_cpf)');
SQL.Add('VALUES(:C, :client, :Cnpj_cpf)');

Then just fill the values in a loop.

SQL.Parameters.ParamByName('C').Value := ADoquery.FieldByName('C').Value;
SQL.Parameters.ParamByName('client').Value := ADoquery.FieldByName('client').Value;
SQL.Parameters.ParamByName('Cnpj_cpf').Value := ADoquery.FieldByName('Cnpj_cpf').Value; 
SQL.ExecSQL;

You can also do the Updade - Insert pattern if the data can alredy be in the target table.

Like This:

if SQL.ExecSQL = 0 then
begin
  // no records were update, do an insert
end;

And also the indication that you are copying data from table 1 to table 2 could be a sign of design flaw. But I can't say that for sure without knowing more. Anyway data duplication is never good.

Runner
A: 

I believe the asker was thinking about the data integrity, that means, ensure that only all the tables will updated or none...

The way I know to achieve this with security is executing all this updates (or inserts, a.s.o.) using SQL commands inside a transition.

Roberto Camargo