in reply to Re: Re: Class::DBI && Sybase
in thread Class::DBI && Sybase
That's when I found some interesting properties of DBD::Sybase that I didn't think would work...
First, if you use placeholders then you can't get at the @@identity value, as is documented.
However, if AutoCommit is off you can do the following (all error handling ommitted!):
This surprised me greatly, to say the least, because I was sure that Sybase wouldn't let me execute the second prepare() while the original $sth was still active.$sth = $dbh->prepare("insert foo(...) values(?, ?, ...)"); # insert first row: $sth->execute('foo', 'bar', ...); # fetch the id value (assuming "foo_id" is an identity column) $sth2 = $dbh->prepare("select max(foo_id) from foo"); $sth2->execute; while($data = $sth2->fetch) { $last_id = $data->[0]; } # insert second row $sth->execute('baz', ...); # fetch the id value $sth2 = $dbh->prepare("select max(foo_id) from foo"); $sth2->execute; while($data = $sth2->fetch) { $last_id = $data->[0]; } # etc.
You need to run this in AutoCommit off mode to make sure that your select max(id) statement will get your last id, not some other processes...
Note that there are locking issues with this technique, as your process holds locks on the table until you execute the commit, which may be a problem for a high volume site.
Michael
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Class::DBI && Sybase
by kal (Hermit) on Apr 03, 2003 at 17:55 UTC |