Home > front end >  Why is this function return empty result when I try to get column name from a table in PostgreSQL?
Why is this function return empty result when I try to get column name from a table in PostgreSQL?

Time:09-27

I have a function that should return all the column name of the table where the table name is passed in as a parameter.

I tried the query without function and it works, however once it is in the function it return empty result.

Here is my function:

CREATE OR REPLACE FUNCTION get_table_columns(tablename text) RETURNS SETOF information_schema.columns AS
$$
    BEGIN
        EXECUTE format('SELECT * FROM information_schema.columns WHERE table_name = %s;', quote_literal(tablename));
    END
$$
LANGUAGE plpgsql;

Here is how I call the function:

SELECT * from get_table_columns('event');

Can anyone help on why it is not returning anything?

Thank you!

CodePudding user response:

You are missing a return query:

BEGIN
  return query EXECUTE format(...);
END
  • Related