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>,"Passynkov, Vadim" <Vadim(dot)Passynkov(at)pathcom(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: get sequence value of insert command
Date: 2004-11-22 14:58:29
Message-ID: C8C8E7457059D5119E4700D0B765DCB8016AA912@sinope.inside.pathcom.com (view raw or flat)
Thread:
Lists: pgsql-sql
> > > 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;
> 
> now that one is really great! you should definitly add it to the
> faq. plus an additional explanation why the limit 1 is needed here.

INSERT INTO foo ( ... ) ( SELECT * FROM foo1 );

> 
> thanks!
> erik
> 

-- 
Vadim Passynkov

pgsql-sql by date

Next:From: Richard RowellDate: 2004-11-22 15:18:13
Subject: Recursive SETOF function
Previous:From: Rod TaylorDate: 2004-11-22 14:10:09
Subject: Re: Doubt :- Image_Insert

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