Question: I want to use a query result as comment on the table. I have tried in psql:

```
\set list select count(*) from department.master_data
COMMENT ON TABLE department.master_data IS :list;

```

But I got this error:

```
it=> \set list select count(*) from department.master_data
it=> COMMENT ON TABLE department.master_data IS :list;
ERROR:  syntax error at or near "selectcount"
LINE 1: ...ABLE department.master_data IS selectcoun...

```

Answer: 
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!

```
test=# COMMENT ON TABLE department.master_data IS :'list';

```

Read about [SQL interpolation in the manual](https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-INTERPOLATION).

Related:

* [CREATE SEQUENCE using expressions with psql variables for parameters](https://dba.stackexchange.com/questions/139681/create-sequence-using-expressions-with-psql-variables-for-parameters/139811#139811)
According to the [manual](https://www.postgresql.org/docs/current/sql-comment.html), the `:list` part should be enclosed in quotation marks:

```
COMMENT ON TABLE  department.master_data
    IS ':list';

```