Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux