Page 1 of 1

Data from insert

Posted: 19.10.2008, 17:47
by QPRocky
Hi.

Is it possible get data during insert?

Code: Select all

+-------+------------------+-----+-----------------+
| Field | Type             | Key | Extra           |
+-------+------------------+-----+-----------------+
|  id   | int(10) unsigned | PRI | auto_increment  |
|  name | varchar(15)      |     |                 |
+-------+------------------+-----+-----------------+


//this returns just 0 or 1, but it would be nice to get actual id
//is it possible?
var id: integer;
.
.
id:=Statement.ExecuteUpdate('INSERT INTO table(name) VALUES "name")');
At moment I insert something first and then serch (select) it to get id.

Posted: 19.10.2008, 18:16
by btrewern
You could try using a TZSequence component. It's used for situations like this.

Ben

Posted: 19.10.2008, 19:02
by mdaems
Actually, I think you better do a 'SELECT LAST_INSERT_ID()' query after the update call. Because of it's simpicity and because using the TZSequence would become a components-based solution, while here the dbc approach is used.

Mark

Posted: 19.10.2008, 20:43
by QPRocky
That SELECT LAST_INSERT_ID() saved my day. :)
Thanks a lot for both answers.

Posted: 24.10.2008, 05:43
by uuid
ZQuery with SQL =

Code: Select all

BEGIN
  INSERT INTO table_profession_rubric (key_profession, key_rubric)
  VALUES (:arg_key_profession, :arg_key_rubric);
  SELECT LAST_INSERT_ID() AS 'out_id';
END
doesn't work as expected. Is it possible not use 2 ZQueries? I'm not sure whether "SELECT LAST_INSERT_ID()" is specific to connection or to something else. I'd like to keep it near the INSERT.

Posted: 24.10.2008, 23:08
by designshouse
try this

with ZQUERY1 do
begin
SQL.Clear;
SQL.Add('INSERT INTO table_profession_rubric (key_profession, key_rubric) ');
SQL.Add('VALUES (:arg_key_profession, :arg_key_rubric); ');
ParamByName('arg_key_profession')...
ParamByName('arg_key_profession')...
ExecSQL;
Close;
SQL.Clear;
SQL.Add('SELECT LAST_INSERT_ID() AS 'out_id'');
Open;
Last_ID := FieldByName('out_id').AsInteger;

end;