Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Autoincrement and server hostname in DB2

by ncw (Friar)
on Oct 06, 2000 at 23:27 UTC ( [id://35644]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://35644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found