Re: new rows based on existing rows

From: Chris Angelico <rosuav(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: new rows based on existing rows
Date: 2012-05-02 17:03:31
Message-ID: CAPTjJmoSr=vGeQ41pfPQpJ-ye6vfQnmq9XNpLy778+2eHPykTg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, May 2, 2012 at 10:52 PM, Andy Chambers <achambers(at)mcna(dot)net> wrote:
> So ideally, I'd like to be able to do
>
> insert into foo (a,b,foo_date)
>  select a,b,now() from foo old where ....
>  returning oid, old.oid
>
> ...but this doesn't work.  It seems you only have access to the table
> being modified in a returning clause.  Is there a way I can return a
> simple mapping between old oids and new oids as part of the statement
> that inserts the new ones?

I'd recommend not using OIDs but having your own ID field (eg a
[BIG]SERIAL PRIMARY KEY). Is the mapping of old ID to new ID something
that would be worth saving into the table? Even if you don't need it
later, that might be the easiest way to do the job. Alternatively, you
could play around with joins (an INSERT RETURNING can quite happily be
used in a WITH clause) to see if you can get what you want that way.

ChrisA

In response to

Browse pgsql-general by date

  From Date Subject
Next Message leaf_yxj 2012-05-02 18:03:47 Re: How to insert random character data into tables for testing purpose. THanks.
Previous Message Radosław Smogura 2012-05-02 16:39:42 Re: Which Java persistence library would you use with PostgreSQL?