In your description you say:
Getting the identity column value of your last insert statement is tricky with DBI.

Do you say this because you tried the "last_insert_id" method in DBI and it didn't work for your particular database server? (If "$dbh" is your DBI handle, then  $dbh->last_insert_id(), with parameters needed for some servers, should return the autoincrement ID from the latest insert.)

Have you checked the DBD module that goes with your particular database server, to see whether there is any mention of an "insert_id" package variable?

It's true that the different database servers (and their respective DBD modules) have different behaviors with regard to the last_insert_id, but I think the issue is documented reasonably well in DBI as well as in the various DBD man pages. I know it works fine for mysql -- I use it all the time.

Apart from that, some people include use warnings (or the "-w" flag) in their scripts, which means that your assignment of a value to $id should probably go like this:

my $id = @idarray ? $idarray[0] : "undef";
so that the following "print" does not set off a warning about "Use of initialized value in print".

In reply to Re: "Getting Sql server identiy column value after insert by graff
in thread "Getting Sql server identiy column value after insert by zhirsr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.