Re: referencing other INSERT VALUES columns inside the insert

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Geoff Winkless <pgsqladmin(at)geoff(dot)dj>
Cc: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: referencing other INSERT VALUES columns inside the insert
Date: 2015-11-16 15:48:53
Message-ID: CAKFQuwaRYYUXyrv5qz7_YOiDMqEmDy-K1nTvosfq_nHh9XMp2A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Nov 16, 2015 at 4:06 AM, Geoff Winkless <pgsqladmin(at)geoff(dot)dj> wrote:

> On 16 November 2015 at 10:55, Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
> wrote:
>
>> What about something along these lines:
>>
>> INSERT INTO test (c1, c2, c3)
>> (WITH fixed(x1, x2) AS (VALUES (3, 7))
>> SELECT x1, x2, x1 * x2 FROM fixed);
>>
>
> ​Genius!
>
> It never occured to me that the with_query parameter could be used that
> way. Thanks!
>
>
​You don't need WITH to accomplish this...

INSERT INTO test (c1, c2, c3)
SELECT c1, c2, c1 * c2
FROM ( VALUES (3, 7) ) vals (c1, c2);

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mohammed Ajil 2015-11-16 16:09:31 Adding a new module to postgres
Previous Message Cj B 2015-11-16 15:46:52 bdr appears to be trying to replicate to itself