I have a postgresql table:
| words | repl |
| word1 | repl1 |
| word2 | repl2 |
| word3 | repl3 |
How can I return a set of all words and repl with stored procedure.
I try:
create function get_words() returns setof text as
$$
declare
r varchar;
begin
for r in
select word,repl from my_table
loop
return next r;
end loop;
return;
end
$$ language plpgsql;
When i execute it i got only word:
select * from get_words();
get_words
-----------
word1
word2
word3
Thank you.
Your function is defined to return only a single column (
returns text). Additionally the variable you are reading the values into is a scalar as well and cannot hold more than one value, so only the word column is put into thervariable.You need to change the function to e.g.
returns set of my_tableand change the definition of the loop variable:If you don’t intend to do anything in the loop using
return querymakes things a bit easier:You can shorten this even further if you don’t use PL/pgSQL but a plain SQL function: