Use \gset
in psql to store the result of a query in a local variable - like dezso already advised. Shorter yet, \gset
can replace the semicolon (;
) as query terminator:
test=# SELECT count(*) AS list FROM department.master_data \gset
Then, to get a single-quoted string (with any contained single quotes safely escaped automatically) refer to the psql variable with this syntax variant: :'list'
. Note the enclosing ''
after the colon! So:
test=# COMMENT ON TABLE department.master_data IS :'list';
Read about SQL interpolation in the manual.
Related: