Re: Insert and obtain the pk

From: "Ian Harding" <ianh(at)tpchd(dot)org>
To: <aruth(at)intercation(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Insert and obtain the pk
Date: 2003-11-06 19:51:50
Message-ID: sfaa35f4.009@mail.tpchd.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Actually, it's tablename_columname_seq , unless that exceeds namedatalen. It shouldn't. Mine never do... But if it does, there is an algorithm for truncating the table and/or field name to fit.

>>> Adam Ruth <aruth(at)intercation(dot)com> 11/06/03 11:46AM >>>
You can call currval on the sequence that provided the value for the
primary key. It's guaranteed to be the last value retrieved from that
sequence *for your session*.

insert into table1 (f1, f2) values ('a', 'b');

select currval('table1_seq');

Use the name of your sequence. If the primary key is a SERIAL type,
then there will be a sequence in the background. I believe it's
tablename_seq or something like that.

regards, adam

On Nov 6, 2003, at 11:26 AM, Eliu Montoya wrote:

>
>
>
>
>
>
> Hello everybody,
>
> Does anyone know an easy way to insert a record (without a value for
> the primary key because it is a sequence) and at the same time obtain
> the value of primary key that was assign to this new record.
>
>
>
> I always try insert and then select with the same values that I used
> for the insert, but I believe this is not a good way to do it.
>
>
>
> Any suggestions???

Browse pgsql-general by date

  From Date Subject
Next Message Boris Popov 2003-11-06 21:48:25 pid of current session?
Previous Message Adam Ruth 2003-11-06 19:46:39 Re: Insert and obtain the pk