TMyQuery with 2 tables - Insert problem
Posted: Mon 12 Feb 2007 23:11
Hello,
My Query joins two tables.
Roughly the sql looks like this:
SELECT AlmostEveryFieldFromTable1, OneFieldFromTable2
FROM Table1
LEFT OUTER JOIN Table2 ON ( Table1.KeyField = Table2.KeyField)
The problem is this:
If the user inserts a new row in the Query, the Autoincrement field of Table1 does not update after post in the application because it consists of two tables.
But I need to insert manually the Table2 as well (two values: this autoinc value and an other number), and to do this I need the autoincrement field value of Table1.
I tried to use SqlInsert with SQLInsert.Add('SELECT @last := LAST_INSERT_ID();'); but in this way I cannot change the value of the second field because I cannot set any parameter to SqlInsert other than the normal fields.
So the summary is:
- if I try to insert using SQLInsert,I can set the autoinc but I cannot set the second parameter (Delphi variable )
- if I try to use the QueryAfterInsert event, I can set the Delphi variable (second parameter), but I cannot set the autoinc value
Is there somehow a solution to this?
Thank you
My Query joins two tables.
Roughly the sql looks like this:
SELECT AlmostEveryFieldFromTable1, OneFieldFromTable2
FROM Table1
LEFT OUTER JOIN Table2 ON ( Table1.KeyField = Table2.KeyField)
The problem is this:
If the user inserts a new row in the Query, the Autoincrement field of Table1 does not update after post in the application because it consists of two tables.
But I need to insert manually the Table2 as well (two values: this autoinc value and an other number), and to do this I need the autoincrement field value of Table1.
I tried to use SqlInsert with SQLInsert.Add('SELECT @last := LAST_INSERT_ID();'); but in this way I cannot change the value of the second field because I cannot set any parameter to SqlInsert other than the normal fields.
So the summary is:
- if I try to insert using SQLInsert,I can set the autoinc but I cannot set the second parameter (Delphi variable )
- if I try to use the QueryAfterInsert event, I can set the Delphi variable (second parameter), but I cannot set the autoinc value
Is there somehow a solution to this?
Thank you