Skip site navigation (1) Skip section navigation (2)

Re: get sequence value of insert command

From: "Passynkov, Vadim" <Vadim(dot)Passynkov(at)pathcom(dot)com>
To: "'Erik Thiele'" <erik(at)thiele-hydraulik(dot)de>,pgsql-sql(at)postgresql(dot)org
Subject: Re: get sequence value of insert command
Date: 2004-11-19 15:57:12
Message-ID: C8C8E7457059D5119E4700D0B765DCB8016AA911@sinope.inside.pathcom.com (view raw or flat)
Thread:
Lists: pgsql-sql

> -----Original Message-----
> From: Erik Thiele [mailto:erik(at)thiele-hydraulik(dot)de]
> Sent: Friday, November 19, 2004 3:42 AM
> To: pgsql-sql(at)postgresql(dot)org
> Subject: [SQL] get sequence value of insert command
> 
> 
> hi
> 
> create sequence mysequence;
> 
> create table foo(
>   id integer default nextval('mysequence'),
>   bla text,
>   wombat integer,
>   foobar date,
>   primary key(id)
> );
> 
> insert into foo (wombat) values (88);
> 
> now how do i know the id of my newly inserted element? and
> how can this be done in a completely concurrency safe way?

CREATE RULE get_pkey_on_insert AS ON INSERT TO foo DO SELECT
currval('mysequence') AS id LIMIT 1;

> 
> 
> cya
> erik
> 
> ---------------------------(end of 
> broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
> 

Responses

pgsql-sql by date

Next:From: Guy FraserDate: 2004-11-19 16:05:03
Subject: Re: Comparing Dates
Previous:From: Riccardo G. FacchiniDate: 2004-11-19 15:54:50
Subject: Re: CREATE TEMPORARY TABLE .. ON COMMIT DROP question

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group