From: | "Bruno BAGUETTE" <pgsql-ml(at)baguette(dot)net> |
---|---|
To: | "'Michal Adamczakk'" <pokryfka(at)artland(dot)com(dot)pl>, <pgsql-general(at)postgresql(dot)org> |
Subject: | RE : mysql's last_insert_id |
Date: | 2003-08-27 12:59:32 |
Message-ID: | !~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAA5ZoowfuKFEe/E2mWA9ac3gEAAAAA@baguette.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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(at)baguette(dot)net
From | Date | Subject | |
---|---|---|---|
Next Message | Bruno Wolff III | 2003-08-27 13:07:09 | Re: pgplsql - Oracle nvl |
Previous Message | Bruno Wolff III | 2003-08-27 12:59:15 | Re: deleting referenced data |