Just another Perl shrine | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
As others have mentioned, DBI has the last_insert_id() method since 1.38. However, I discovered as I was trying to implement this for DBD::Sybase that it is broken up to and including the current 1.42 release. The bug is that a flag in the DBI dispatcher is incorrect so that DBI never calls the underlying DBD's implementation of last_insert_id().
In addition, last_insert_id() will only work if the underlying DBD has implemented it.
As you have discovered getting this value from the various database server engines requires some very different approaches. The most generic approach is to simply do a SELECT max(idvalue) FROM table right after the insert. If you are running with AutoCommit OFF, and if the server supports transactions then this should return the correct value, as long as you COMMIT after the SELECT max(). Otherwise this is open to a race condition in the case of multiple inserts to that same table. And FYI - for Sybase and MS-SQL the last inserted id (IDENTITY value) can be retrieved with a SELECT @@identity. Michael In reply to Re: Auto-Increment and DBD Agnosticism
by mpeppler
|
|