http://qs1969.pair.com?node_id=35644


in reply to Autoincrement and server hostname in DB2

There isn't a DBI standard method of getting the id of a last autoincremented id as far as I know. I think this is because not all databases support this method of doing things.

That said I know how to do this in MySQL. After you have done

$cursor = $dbh->prepare("insert into table (a, b) values (?, ?)"); $cursor->execute($a_var, $b_var);
You can then read the last autoincrementid using a MySQL extension
$id = $dbh->{mysql_insertid};
This variable is related to your dbh session so it is safe in the presence of multiple updaters.

In a previous version of DBD::mysql the variable was just $dbh->{insertid}.

Don't try to get the last inserted id by "select max(id) from table" this will work short term, but get you into no end of trouble later. You could make it work by locking the table before your insert I guess.

It would be useful if this was standardised in DBI if possible. I personally build all my tables using autoincrementing id's and $dbh->{mysql_insertid} for joining them.

Look at your documentation for DBD::DB2 and see if it offers anything similar!