Re: Any way to insert rows with ID used in another column

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Ben Hoyt <benhoyt(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Any way to insert rows with ID used in another column
Date: 2014-04-03 15:19:56
Message-ID: CAF-3MvN1n2BweOk2mpRndViE0RODS84_=BS1sqjWGzUrQLcaag@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 3 April 2014 16:41, Ben Hoyt <benhoyt(at)gmail(dot)com> wrote:
> Hi folks,

> I tried using currval() to see if that'd work, but it gave an error, I guess
> because I was using it multiple times per session.

currval() requires that nextval() was called before it (either
automatically or explicitly) in the same transaction.

Usually what you want is achieved using nextval(). You request n new
ID's using nextval(), which you can then use to both name your n image
files and for the ID with which you will be inserting them into your
table.

Unfortunately there doesn't appear to be a variant of nextval() that
you pass a number which then subsequently returns a set of values,
that would be ideal for such usage, but that can be worked around by
calling nextval() in conjunction with generate_series().

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Schnabel, Robert D. 2014-04-03 15:27:53 window function help
Previous Message Francisco Olarte 2014-04-03 15:17:42 Re: Any way to insert rows with ID used in another column