Luca Abbati
Luca Abbati

Reputation: 572

PostgreSQL 9.1: set comment on table with returning value from a function

If I use this piece of code to write the comment on a TABLE everything works fine:

COMMENT ON TABLE schemaname.tablename IS 'Some Comment';

But if I want to use a function's return value as the value of the comment I have an error, like here:

COMMENT ON TABLE schemaname.tablename IS substring('Thomas' from 2 for 3);

ERROR:  syntax error at or near "substring"

Any idea on how to fix this? (I would not like to edit the 'pg_catalog.pg_description' system table)

Thank you. Luca

Upvotes: 1

Views: 78

Answers (1)

user330315
user330315

Reputation:

You need dynamic SQL for that:

do
$body$
declare
  comment_string text;
begin
  comment_string := substr('thomas', 2, 3);
  execute 'comment on table public.foo is '||quote_literal(comment_string);
  commit;
end;
$body$

Upvotes: 1

Related Questions