Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How do I find what column $dbh-{'insert_id'} was inserted into?

by artful (Initiate)
on Feb 14, 2001 at 03:56 UTC ( [id://58259]=perlquestion: print w/replies, xml ) Need Help??

artful has asked for the wisdom of the Perl Monks concerning the following question:

When doing an insert into a table with an auto_increment field, you can get the auto_incremented field with $dbh->{'insert_id'}. Is there an easy query or function to find out what column this value was inserted into? Do I have to do a describe table and look for the primary key, or is there an easier way?
  • Comment on How do I find what column $dbh-{'insert_id'} was inserted into?

Replies are listed 'Best First'.
Re: How do I find what column $dbh-{'insert_id'} was inserted into?
by kschwab (Vicar) on Feb 14, 2001 at 04:55 UTC
    It looks to be experimental at this point, but have a look at the STATEMENT HANDLE ATTRIBUTE area in the DBI docs.

    You could loop thru the results of $sth->{TYPE} and look for something with AUTO_UNIQUE_VALUE. (you would get the integer equiv to AUTO_UNIQUE_VALUE from the type_info* methods)

Re: How do I find what column $dbh-{'insert_id'} was inserted into?
by dvergin (Monsignor) on Feb 14, 2001 at 12:22 UTC
    The function you need is LAST_INSERT_ID().

    Immediately after the insert, execute a SELECT LAST_INSERT_ID() as if it were a normal select statement in DBI. The value you retreive will be the auto_increment column value of your recent insert.

    This is covered in the DuBois book (pp. 93, 170, 547) and in the MySQL online docs under 'Miscellaneous Functions'.

    (Note that mysql_insert_id() is part of the C API and is not directly accessable from Perl.)

      The function you need is LAST_INSERT_ID().
      I think this is particular to MySQL. For Sybase and Oracle, also try
      SELECT @@IDENTITY FROM table
      My projects have always had this statement in a transaction, but I'm not sure if it's needed.
Re: How do I find what column $dbh-{'insert_id'} was inserted into?
by wardk (Deacon) on Feb 14, 2001 at 20:39 UTC

    artful, Appears you are curious as what the name of the column that contains the incremented value is...not what the value of the last insert was from the previous insert.

    Sounds like you are looking for a description of the table itself. Since you mention describe, I am thinking you are talking about Oracle. (although as you can see from the forthcoming example, via oracle you would(should) use the sequenced column in the insert..so maybe I assume wrong) Anyway, here is an oracle way...

    a typcial sequence that is tied to a column during an insert:

    create sequence seq_name start with 1 increment by 1 maxvalue 999999 nocycle cache 20; create public synonym seq_name for owner.seq_name; grant select on seq_name to joe_user;

    With above created, you now do the insert like so:

    insert into table (seq_col, cola, colb, colc) values (seq_name.NEXTVAL, 'data','moredata','otherdata')

    Now I guess it's quite possible that the technique currently being used does not list the columns prior to the values...ala

    insert into table values (seq_name.NEXTVAL, 'data','moredata','otherdata')

    ...therefore you are not seeing the column name...if this is the case, modify the sql to use all the columns (you'll probably need the describe at this point...but just once, since you now have the column name in the sql). Note that doing an insert "by position" (without naming the columns prior to values) can get you into trouble if the table is ever altered without warning (that never happens, eh? :-))

    anyway hope I was at least close on this one...

    good luck

Re: How do I find what column $dbh-{'insert_id'} was inserted into?
by artful (Initiate) on Feb 14, 2001 at 23:31 UTC
    Thanks for all of the help. I appologize because I should have actually said $dbh->{'mysql_insertid'}, sorry for the confusion. What I ended up doing is writting a quick function to look at the table and return the first auto_increment field it found.

    This is what I came up with:
    sub get_auto_increment { my ($dbh, $table) = @_; my $sth = $dbh->prepare("show columns from $table"); $sth->execute; my $ref; while ($ref = $sth->fetchrow_hashref) { if ($ref->{'Extra'} eq 'auto_increment') { $sth->finish; return $ref->{'Field'}; } } $sth->finish; return undef; }

    The idea is that the first column that matches 'auto_increment' will be the column that $dbh->{'mysql_insertid'} is referring to. I am not sure what the rules are for mysql's auto_increment definition. If I am not mistaken (which I probably am) all columns marked auto_increment would get the same value anyway?

    Thanks,
    -Art-
      Actually, MySQL only allows one auto_increment column per table. So your method should work fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://58259]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found