TSQLQuery is derived of TBufdataset. TBufdataset is a memorytable that's hold your data.
If TSQLQuery.SQL.Text = 'SELECT id,name,age OF table1' is your mainquery, You can create
TSQLQuery.UpdateSQL.Text := 'INSERT INTO table2 (id,name,age) VALUES (:id,:name,:age)'
TSQLQuery.InsertSQL.Text := 'INSERT INTO table2 (id,name,age) VALUES (:id,:name,:age)'.
After using TSQLQuery.ApplyUpdates TSQLQuery will use InsertSQL and UpdateSQL to write queries to save the records to another table.
But the best thing is to create a new TSQLQuery components which writes all your records sequently to your new table.
procedure SaveToOtherTable;
var MyInsertQuery : TSQLQuery;
MyTransAction : TSQLTransaction;
begin
MyInsertQuery := TSQLQuery.create(nil);
MyTransAction := TSQLTransaction.create(nil);
try
MyInsertQuery.connection := <your database connection>;
MyTransAction.connection := <your database connection>;
MyInsertQuery.Transaction := MyTransAction;
MyInsertQuery.SQL.Text := 'INSERT INTO table2 (name,age) VALUES (:name,:age)';
MySelectQuery.first;
while not MySelectQuery.eof do
begin
MyInsertQuery.Params[0].AsString := MySelectQuery.fields[0].AsString;
MyInsertQuery.Params[1].AsInteger := MySelectQuery.fields[1].AsInteger;
MyInsertQuery.ExecSQL;
MySelectQuery.Next;
end;
MyInsertQuery.ApplyUpdates;
MyTransAction.Commit;
finally
MyInsertQuery.free;
MyTransAction.free;
end;
end;