From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: returning setof from insert ?
Date: Wed, 14 Jul 2021 14:14:44 +0200
Message-ID: <4b2c7447-346a-8d1b-71bd-d55c5e183007@gmx.net> (raw)
In-Reply-To: <VTfbqJ6-qhwCDDq0TYhdA4XuNH0hsJSqMX6KOBTY-D_kxclXuEV2gwSaCQA3HTkm698pCg4V7qyTVTzEphlgfowGQEVAJvfggC9xqbJ8CZ8=@protonmail.ch>
References: <VTfbqJ6-qhwCDDq0TYhdA4XuNH0hsJSqMX6KOBTY-D_kxclXuEV2gwSaCQA3HTkm698pCg4V7qyTVTzEphlgfowGQEVAJvfggC9xqbJ8CZ8=@protonmail.ch>
Laura Smith schrieb am 14.07.2021 um 13:22:
> A bit of pl/pgsql writer's block going on here ...
>
> Postgres complains "RETURN cannot have a parameter in function returning set" in relation to the below. I don't really want to have to "RETURNS TABLE" because that means I have to enumerate all the table columns.
>
> I'm sure I'm missing something simple here !
>
> CREATE OR REPLACE FUNCTION foobar(foo text,bar text) RETURNS SETOF bar AS $$
> DECLARE
> v_row bar%ROWTYPE;
> BEGIN
> insert into bar(f,b) values(foo,bar) returning * into v_row;
> return v_row;
> END;
> $$ language plpgsql;
>
You need to use RETURN NEXT:
CREATE OR REPLACE FUNCTION foobar(foo text,bar text)
RETURNS SETOF bar
AS $$
DECLARE
v_row bar%ROWTYPE;
BEGIN
insert into bar(f,b) values(foo,bar) returning * into v_row;
return next v_row;
END;
$$
language plpgsql;
But you don't need PL/pgSQL for this or store the result in a variable:
CREATE OR REPLACE FUNCTION foobar(foo text,bar text)
RETURNS SETOF bar
AS $$
insert into bar(f,b) values(foo,bar)
returning *;
$$
language sql;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: shammat@gmx.net
Subject: Re: returning setof from insert ?
In-Reply-To: <4b2c7447-346a-8d1b-71bd-d55c5e183007@gmx.net>
References: <VTfbqJ6-qhwCDDq0TYhdA4XuNH0hsJSqMX6KOBTY-D_kxclXuEV2gwSaCQA3HTkm698pCg4V7qyTVTzEphlgfowGQEVAJvfggC9xqbJ8CZ8=@protonmail.ch> <4b2c7447-346a-8d1b-71bd-d55c5e183007@gmx.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox