1
create or replace function extr( tabname text ) returns text[] as
$$
declare cols text[];
begin
    execute 'array(select column_name::text from information_schema.columns where table_name = '||quote_literal(tabname)||');' into cols;
    return cols;
end;
$$
language 'plpgsql';

select extr('test');

One supplies a table name and wants back its column-names as an array. The above code gives 'syntax error at or near "array"'. How to fix this?

1 Answer 1

3

The query should start with select, not array and it doesn't have to be dynamic SQL.

Try this modified version:

create or replace function extr( tabname text ) returns text[] as
$$
declare cols text[];
begin
    select array(select column_name::text from information_schema.columns
      where table_name = tabname) into cols;
    return cols;
end;
$$
language 'plpgsql';
Sign up to request clarification or add additional context in comments.

Comments

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.