Get last serial (autoincrement) value
Get last serial (autoincrement) value
am 01.09.2003 12:34:32 von andreil1
Hi,
Anyone knows how to obtain last inserted serial (autoincrement) id? I
am currently using "SELECT id FROM table_name ORDER BY id DESC", and
then retrieve first row and desired value.
May be there is more elegant way? I am using ADODB. ADODB has function
which in theory allows to obtain
last inserted serial (autoincrement) id, but for Postgres it return OID.
This is from ADODB manual:
Insert_ID( )
Returns the last autonumbering ID inserted. Returns false if function
not supported.
Only supported by databases that support auto-increment or object id's,
such as PostgreSQL, MySQL and MSSQL currently. PostgreSQL returns the
OID, which can change on a database reload.
Thanks in advance for any suggestion.
*********************************************
* Best Regards --- Andrei Verovski
*
* Personal Home Page
* http://snow.prohosting.com/guru4mac
* Mac, Linux, DTP, Development, IT WEB Site
*********************************************
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly
Re: Get last serial (autoincrement) value
am 01.09.2003 20:11:06 von David Busby
select currval('table_id_column_seq');
----- Original Message -----
From: "Andrei Verovski (aka MacGuru)"
To:
Sent: Monday, September 01, 2003 03:34
Subject: [PHP] Get last serial (autoincrement) value
> Hi,
>
> Anyone knows how to obtain last inserted serial (autoincrement) id? I
> am currently using "SELECT id FROM table_name ORDER BY id DESC", and
> then retrieve first row and desired value.
>
> May be there is more elegant way? I am using ADODB. ADODB has function
> which in theory allows to obtain
> last inserted serial (autoincrement) id, but for Postgres it return OID.
>
> This is from ADODB manual:
>
> Insert_ID( )
> Returns the last autonumbering ID inserted. Returns false if function
> not supported.
> Only supported by databases that support auto-increment or object id's,
> such as PostgreSQL, MySQL and MSSQL currently. PostgreSQL returns the
> OID, which can change on a database reload.
>
>
> Thanks in advance for any suggestion.
>
>
> *********************************************
> * Best Regards --- Andrei Verovski
> *
> * Personal Home Page
> * http://snow.prohosting.com/guru4mac
> * Mac, Linux, DTP, Development, IT WEB Site
> *********************************************
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
Re: Get last serial (autoincrement) value
am 01.09.2003 20:48:30 von Frank Bax
At 06:34 AM 9/1/03, aka MacGuru wrote:
>Anyone knows how to obtain last inserted serial (autoincrement) id? I am
>currently using "SELECT id FROM table_name ORDER BY id DESC", and then
>retrieve first row and desired value.
Faster then your version:
SELECT id FROM table_name ORDER BY id DESC LIMIT 1
The 'elegant' solution:
SELECT currval('tablename_sequence_id');
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
Re: Get last serial (autoincrement) value
am 01.09.2003 21:01:22 von Bruno Wolff III
On Mon, Sep 01, 2003 at 13:34:32 +0300,
Andrei Verovski wrote:
>
> Anyone knows how to obtain last inserted serial (autoincrement) id? I
> am currently using "SELECT id FROM table_name ORDER BY id DESC", and
> then retrieve first row and desired value.
How you want to do this, depends on what you what to do with the number.
Normally if you just inserted a record that would create a new id and you
want to use that id to enter in another table, then you want to call
currval.
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly