On Mon, 2003-03-10 at 12:36, Tim Bunce wrote:
> On Mon, Mar 10, 2003 at 08:37:45AM -0800, Michael Peppler wrote:
> > > > 
> > > > > > Note that you *can't* get at the @@identity value if you insert data
> > > > > > using placeholders. This is because Sybase also localizes the
> > > > > > @@variables within each stored procedure, and when using placeholders
> > > > > > Sybase creates a temporary stored procedure for you on the fly.
> 
> So the @@variables can't even be read by another stored procedure?
> If so, that seems remarkably unhelpful.

The idea is to be able to do nested procedure calls and not have a
called proc affect the @@variables in the caller (such as @@error,
@@identity, @@rowcount, etc). It makes sense in most situations, but
it's a problem here.

> 
> Previously you said:
> > Off-hand I'm not sure how a last_insert_id() method would work for
> > DBD::Sybase, but there may be a way to build something that is at least
> > semi-reliable.
> So, any ideas?
> 
> Taking a different tack... *if* @@identity is a sequence and AutoCommit
> is off then then "select max($column) from $table" would get it. No?

Yes, that would work.

However, the following won't:

$sth = $dbh->prepare('insert foo(...) values(?,...)');
$sth->execute('bar', ...);
$id_sth = $dbh->prepare('select max(id) from foo');

because now $id_sth resides on a different physical connection. You'd
have to finish() the original $sth, and then do the second prepare() to
get the max() value, which is of course rather annoying if you're doing
a bunch of inserts (though it's maybe unlikely that you really need the
last_id value if you're doing a batch of inserts).

Michael
-- 
Michael Peppler                              Data Migrations, Inc.
[EMAIL PROTECTED]                 http://www.mbay.net/~mpeppler
Sybase T-SQL/OpenClient/OpenServer/C/Perl developer available for short or 
long term contract positions - http://www.mbay.net/~mpeppler/resume.html

Reply via email to