1

I just defined this function to return all column names from a given table:

create or replace function GET_COLUMNS(in tbl_name character varying(30))
returns table(colunas character varying) as $$
begin
 SELECT column_name
 FROM information_schema.columns
 WHERE table_schema = 'Main'
 AND table_name = tbl_name;
end;
$$ language 'plpgsql'

But When I call it using select * from get_columns('tabfuncionarios'); I just got the following error:

ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function get_columns(character varying) line 3 at SQL statement

I'm using postgresql 9.4.5 version

Erwin Brandstetter
186k28 gold badges463 silver badges636 bronze badges
asked Dec 5, 2015 at 15:52
1
  • One more thing: It hardly makes sense to define the input parameter as character varying(30). Just use varchar (without maximum length) or text. Commented Dec 6, 2015 at 5:22

2 Answers 2

4

You just need to add RETURN QUERY to the start of your query:

create or replace function GET_COLUMNS(in tbl_name character varying(30))
returns table(colunas character varying) as $$
begin
 RETURN QUERY
 SELECT column_name
 FROM information_schema.columns
 WHERE table_schema = 'Main'
 AND table_name = tbl_name;
end;
$$ language plpgsql
Erwin Brandstetter
186k28 gold badges463 silver badges636 bronze badges
answered Dec 5, 2015 at 16:08
0
6

You don't need plpgsql for this. A plain sql function will do:

create or replace function GET_COLUMNS(in tbl_name character varying(30))
 returns table(colunas character varying) 
as 
$$
 SELECT column_name
 FROM information_schema.columns
 WHERE table_schema = 'Main'
 AND table_name = tbl_name;
$$ 
language sql;

Additionally: the language name is an identifier. Don't put it in single quotes.

answered Dec 5, 2015 at 16:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.