1

Imagine we have a simple table

create table a (id serial, data text);

and the following function:

create or replace function f()
returns SETOF int As
$BODY$
DECLARE
  l_arr int[];
BEGIN

  insert into a(data) 
  values ('a') 
  returning array_agg(data) into l_arr; --THIS DOES NOT WORK

  RETURN l_arr;

END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
  COST 100

How one may gather inserted values into an array in the same SQL statement?

1 Answer 1

1

It appears, that it is possible to use RETURN QUERY without intermediate array:

create or replace function f()
returns SETOF int As
$BODY$
BEGIN
  RETURN QUERY
  insert into a(data) 
  values ('a') 
  returning id;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER
  COST 100
Sign up to request clarification or add additional context in comments.

2 Comments

This will return a set, not an array, so this is not a good answer for your question :)
@dezso it is the answer because the function in the question aims to return set ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.