Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k19fB-00027N-8X for pgsql-novice@arkaria.postgresql.org; Thu, 30 Jul 2020 14:36:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k19fA-0005v5-7F for pgsql-novice@arkaria.postgresql.org; Thu, 30 Jul 2020 14:36:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k19fA-0005uy-0p for pgsql-novice@lists.postgresql.org; Thu, 30 Jul 2020 14:36:04 +0000 Received: from tamriel.snowman.net ([96.255.250.162]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k19f8-00041w-27 for pgsql-novice@lists.postgresql.org; Thu, 30 Jul 2020 14:36:03 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id A50965F79F; Thu, 30 Jul 2020 10:36:00 -0400 (EDT) Date: Thu, 30 Jul 2020 10:36:00 -0400 From: Stephen Frost To: Chamath Sajeewa Cc: Tom Lane , pgsql-novice@lists.postgresql.org Subject: Re: PGSQL returning into in insert statement Message-ID: <20200730143600.GX12375@tamriel.snowman.net> References: <1352566.1596078339@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="j1GQOFr+JpStR0Db" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --j1GQOFr+JpStR0Db Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Chamath Sajeewa (csgsajeewa@gmail.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: =3D*> 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=20 ------------ 5 (1 row) Which you can then query with: =3D*> select current_setting('myvar.value'); current_setting=20 ----------------- 5 (1 row) Thanks, Stephen --j1GQOFr+JpStR0Db Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJfItrQAAoJEO1sijiDR2RVVkUP/2qBzBwm1/ucjsvlNfOgaZ6p v7zFjBwduvJrMRSESyDvTjhTJIcn0GSykJIO1PEU4oyp0avWs43V/K2kKc0Ep7iI Tnfk+qxgg1sK99RRqg1s6oXriJCwTMUWK8KlddNHDvGqjd6fFw9zzj8B22XhUe4w XJi2eVAuxdrz9w4YOkU5lkoHb8I4iklzTjCtCAXOmdgg/HIN1R3+96Ifrxoqtxoz busz3RsNjzeRAoI+kXgbZ0admoeb3a7572iGLZKZaoeVklqLQ+UU78FlJ0ZyvIBN rzN9zF/89KVXBIjmgnFD3RBv0yechcXeM0oxwB1qiMFMI7c4QJZFmvF7prJ1KuD1 +MP9QCKMKjHQCi3vSqAfJiBC5Mxr4Fh7Aox/V734wCojVLgjnT9ni+1stzQm0R9n 3ZR2rSn+3qh6LizQoBHuRQQz40DfaotBZa+xZC2R7Hyeyxu+6MpDih51wI2wWPFj IQJMVlsbkUB+0D4TQKuUDMcDeKbmRAXWhNTFdM/jJA5LzNDpvG9ZM5mp3tJnXX1a 029HjtNopXCEHJOIjhD8lHNjMnfsuNu7fRhGzqCOMzluCr0tqzW4XovNM/uaoyi6 dS+xdQbrRo/IJ5UVUy9ceDQ0yJyLzf11Z8ILaZAVExEJxdUFjkBQg7CibSjJ2PL5 +4wCAUbM08sH/ZN2tmwo =Tr7q -----END PGP SIGNATURE----- --j1GQOFr+JpStR0Db--