Re: PGSQL returning into in insert statement

From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Chamath Sajeewa <csgsajeewa(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: PGSQL returning into in insert statement
Date: 2020-07-30 14:36:00
Message-ID: 20200730143600.GX12375@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Greetings,

* Chamath Sajeewa (csgsajeewa(at)gmail(dot)com) wrote:
> Btw oracle support this in plain sql too.

Please don't top-post.

The question here, it would seem, is- where do you expect that
returnvalue to go? If you want it to be returned to you (making the
INSERT look like a SELECT) then you can just say 'returning value;' and
you don't need to do anything else.

If you want to set a server-side variable with the result, you could do
something like:

=*> with myinsert as
(insert into test_table(key,value) values ('key1',5) returning value)
select set_config('myvar.value'::text,myinsert.value::text,true)
from myinsert;
set_config
------------
5
(1 row)

Which you can then query with:

=*> select current_setting('myvar.value');
current_setting
-----------------
5
(1 row)

Thanks,

Stephen

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message msm.scaglione 2020-07-31 09:17:24 Re: configuration problems with Debian and derivates [solved]
Previous Message Stephen Frost 2020-07-30 14:28:52 Re: PGSQL insert data to a oid column using SOCI