Re: How I can read-back a serial value just inserted?

Поиск
Список
Период
Сортировка
От Madison Kelly
Тема Re: How I can read-back a serial value just inserted?
Дата
Msg-id 45AF0B64.1090509@alteeve.com
обсуждение исходный текст
Ответ на How I can read-back a serial value just inserted?  ("dfx" <dfx@dfx.it>)
Список pgsql-general
dfx wrote:
> Dear Sirs,
>
> my question is very simple:
> when I insert a row whith a serial field, a value is automatically
> generated; how  can I know this value, strictly of my row, without the risk
> of to read the value of another subsequent insertion?
>
> Thank you.
>
> Domenico

Hiya,

   Not sure if it will help you, but what I do is:

SELECT nextval('pt_seq'::regclass);

Take the returned value and use it in the INSERT statement. So for
example, with the sequence/table:

CREATE SEQUENCE pt_seq
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;
ALTER TABLE pt_seq OWNER TO digimer;

CREATE TABLE part_type (
    pt_id    int    primary key    default(nextval('pt_seq')),
    pt_name    text,
    ...
);
ALTER TABLE part_type OWNER TO digimer;

I would do (in perl, but other languages should be similar enough):

my $pt_id=$dbh->selectrow_array("SELECT nextval('pt_seq'::regclass)");
$dbh->do("INSERT INTO part_type (pt_id, pt_name...) VALUES ($pt_id,
'$name'...)");

Hope that helps!

Madi

В списке pgsql-general по дате отправления:

Предыдущее
От: Bruno Wolff III
Дата:
Сообщение: Re: How I can read-back a serial value just inserted?
Следующее
От: "Adam Rich"
Дата:
Сообщение: Re: How I can read-back a serial value just inserted?