in reply to Re: mssql query
in thread mssql query

As long as the insert and the select @@identity happen sequentially on the same connection you'll be fine:
$dbh->do("insert ... "); $sth = $dbh->prepare('select @@identity'); $sth->execute(); my $data = $sth->fetch;...
What will NOT work is selecting the @@identity value IF the insert happened in a stored procedure, because the @@identity variable is localized. So the following:
$dbh->do("exec some_proc_with_an_insert 1,2,3"); $sth = $dbh->prepare('select @@identity'); $sth->execute(); my $data = $sth->fetch;
will NOT return the @@identity value generated in the stored procedure. This is the reason why you can't get the @@identity value after doing an insert with placeholders (at least with Sybase): any statement with placeholders generates a temporary stored procedure, hence localizing the @@identity variable.

Michael

Replies are listed 'Best First'.
Re: Re: Re: mssql query
by demerphq (Chancellor) on Dec 02, 2003 at 00:08 UTC

    Ah. Ok I stand corrected. Thanks for the heads up.

    Cheers mate. :-)


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi