Hello!

I ask for help, I can not figure out the code. There are two tables (Table1 and Table2), so from the second table on the first two lines should be copied (Imya and Fam), and for some reason I copy everything that is in the second table. I program on Delphi an Access DB. Help me please. Here is the code:

ADOQuery1.Active:=false; ADOQuery1.SQL.Clear; ADOQuery1.SQL.add('INSERT INTO Таблица1 (Imya, fam, Adres, Nom) '); ADOQuery1.SQL.add('SELECT Imya, fam, Adres, Nom'); ADOQuery1.SQL.Add('FROM Таблица2'); ADOQuery1.ExecSQL; ADOQuery1.SQL.Text:='SELECT * FROM Таблица2'; 
  • 2
    Read about the sql, in this code nothing is really clear, but you make a complete SELECT * sample, and insert all the fields ( Imya, fam, Adres, Nom ) - Gorets

2 answers 2

Maybe so? :)

 ADOQuery1.Active:=false; ADOQuery1.SQL.Clear; ADOQuery1.SQL.add('INSERT INTO Таблица1 (Imya, fam) '); ADOQuery1.SQL.add('SELECT Imya, fam'); ADOQuery1.SQL.Add('FROM Таблица2'); ADOQuery1.ExecSQL; ADOQuery1.SQL.Text:='SELECT * FROM Таблица1'; 

ADOQuery1.SQL.Text: = 'SELECT * FROM Table1 ';

  • Zivyanki lured, (the address and the number are not needed) I have it: ADOQuery1.Active:=false; ADOQuery1.SQL.Clear; ADOQuery1.SQL.add('INSERT INTO Таблица1 (Imya, fam) '); ADOQuery1.SQL.add('SELECT Imya, fam'); ADOQuery1.SQL.Add('FROM Таблица2'); ADOQuery1.ExecSQL; ADOQuery1.SQL.Text:='SELECT * FROM Таблица1'; ADOQuery1.Active:=false; ADOQuery1.SQL.Clear; ADOQuery1.SQL.add('INSERT INTO Таблица1 (Imya, fam) '); ADOQuery1.SQL.add('SELECT Imya, fam'); ADOQuery1.SQL.Add('FROM Таблица2'); ADOQuery1.ExecSQL; ADOQuery1.SQL.Text:='SELECT * FROM Таблица1'; But why is the entire table being copied, if there are three people in the table, then it copies them all, but I need to copy only one ?! - ruslan911
  • Add the necessary WHERE clause: ADOQuery1.SQL.Add ('WHERE Imya = "123"'); I do not know the exact syntax, but I think it should be clear. - Nord001

It seems you do not understand how to implement queries to the database. If you need a sample, that is, you need to get something as a result, (field, columns, etc.), then you need instead of ExecSql; just open open Open; It is logical to use ExecSql when updating, writing, or deleting from the database (update, insert, delete). And if we say

 AdoQuery.sql:='select * from table'; Adoquery.ExecSql;//запрос выполниться 1 раз,PS: это 'лишняя' Adoquery.open;//запрос выполниться 2 раз 

In your case, the logic should be:

 ADOQuery1.SQL.Clear; ADOQuery1.SQL.add('INSERT INTO Таблица1 (Imya, fam) '); ADOQuery1.ExecSQL; ADOQuery1.SQL.Clear; ADOQuery1.SQL.add('SELECT Imya, fam'); ADOQuery1.SQL.Add('FROM Таблица2'); ADOQuery1.Open;