RE : mysql's last_insert_id - Mailing list pgsql-general

From Bruno BAGUETTE
Subject RE : mysql's last_insert_id
Date
Msg-id !~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAA5ZoowfuKFEe/E2mWA9ac3gEAAAAA@baguette.net
Whole thread Raw
In response to mysql's last_insert_id  (Michal Adamczakk <pokryfka@artland.com.pl>)
List pgsql-general
Hello,

> how to implement mysql's last_insert_id() ?
>
> i know that seqences and oids are great.
> the one thing i miss is that they are not session specific.
> i mean selecting last_value from seqence can give me a value
> which was inserted by a different user.

Am I wrong or do you misunderstand the documentation about sequences ?

If you read the documentation about the sequences functions
(http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=functio
ns-sequence.html), you can see that the currval functions return the
value most recently obtained by nextval for this sequence IN THE CURRENT
SESSION. (An error is reported if nextval has never been called for this
sequence in this session.) Notice that because this is returning a
session-local value, it gives a predictable answer even if other
sessions are executing nextval meanwhile.

So, if you need to do two inserts where you have to add the new added
sequence value, you can do something like :

INSERT INTO authors (pk_author_id,lastname,firstname) VALUES
(NEXTVAL('seq_author_id'),'Baguette','Bruno');
INSERT INTO books (fk_author_id,title) VALUES
(CURRVAL('seq_author_id'),'Deafness related bibliography');

Hope this helps :-)

Regards,

---------------------------------------
Bruno BAGUETTE - pgsql-ml@baguette.net


pgsql-general by date:

Previous
From: Robby Russell
Date:
Subject: Re: SQL Command - To List Tables ?
Next
From: Jeffrey Melloy
Date:
Subject: Re: SQL Command - To List Tables ?