I tried to concatenate a longer comment over several lines to keep my code clean and was surprised to find that I can't. For example, the following returns a syntax error at the pipes:
COMMENT ON VIEW my_view IS 'foo' || 'bar';
Is there something special about comments?
Syntax Using /* and */ symbols In PostgreSQL, a comment that starts with /* symbol and ends with */ and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.
The PostgreSQL concatenate operator ( || ) is used to concatenate two or more strings and non strings.
PostgreSQL allows you to directly concatenate strings, columns and int values using || operator. Here is the SQL query to concatenate columns first_name and last_name using || operator. You can even concatenate string with int using || operator.
The documentation explains about the argument:
The new comment, written as a string literal; or
NULL
to drop the comment.
So you cannot have an expression in that place, only a constant.
Fortunately the SQL standard has decreed that adjacent string literals be concatenated, so you can write
COMMENT ON VIEW my_view IS 'foo'
'bar';
The documentation states that only text and no expressions are allowed:
COMMENT ON { ACCESS METHOD object_name | ... VIEW object_name } IS 'text'
Note the apos around text. But you may use dynamic created statements for that:
DO $$
BEGIN
EXECUTE FORMAT('COMMENT ON VIEW myview IS %L', 'A very long ' || 'comment');
END
$$ LANGUAGE PLPGSQL;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With