From: | Madison Kelly <linux(at)alteeve(dot)com> |
---|---|
To: | dfx(at)dfx(dot)it |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: How I can read-back a serial value just inserted? |
Date: | 2007-01-18 05:53:40 |
Message-ID: | 45AF0B64.1090509@alteeve.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
From | Date | Subject | |
---|---|---|---|
Next Message | Adam Rich | 2007-01-18 05:57:41 | Re: How I can read-back a serial value just inserted? |
Previous Message | Bruno Wolff III | 2007-01-18 05:49:09 | Re: How I can read-back a serial value just inserted? |