Re: How do I select the last Id in a column??? - Mailing list pgsql-admin

From scott.marlowe
Subject Re: How do I select the last Id in a column???
Date
Msg-id Pine.LNX.4.33.0306261512050.1660-100000@css120.ihs.com
Whole thread Raw
In response to How do I select the last Id in a column???  (Michael Kovalcik <makd32@yahoo.com>)
List pgsql-admin
The proper way to do this is basically this:

begin;
insert into table test values ('abc',123);
select currval('sequsedbytest');
insert into dependent_table values (keyfromabovecurrval,'moredata',456);
commit;

On Mon, 23 Jun 2003, Michael Kovalcik wrote:

> Hello,
>
> I'm trying to find out how I select the last id in a
> column if I don't know anything else about the row.
> What i am doing is inserting information into my
> Orders table and the id is auto-incrementing.  The
> next thing i want to be able to do is grab the the id
> of the order i just inserted.  How do i do this?
>
> Here is the code i have so far:
>
> my $sql = "INSERT INTO ${Orders}
> (locationid,shippingid,statusid,date) VALUES (?
> ,?,?,?)";
>
> my $sth = $dbh->prepare($sql);
>
> $sth->execute(${LocationID},${ShippingID},${StatusID},${date});
> #--*************************************
> my $sth = $dbh->prepare("SELECT id FROM ${Orders}");
>
> $sth->execute();
> $OrderID = $sth->fetchrow_arrayref;
>
> I know the select statement that i have will not work,
> because it will bring me every id number in the table.
>  I just want the latest one.....
>
> Please HELP............
>
> Thank you so much,
> Mike
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>


pgsql-admin by date:

Previous
From: Ericson Smith
Date:
Subject: Re: How do I select the last Id in a column???
Next
From: DHS Webmaster
Date:
Subject: Re: How do I select the last Id in a column???