What I want is for the same query not to fail - regardless of the variable being set or not. Then depending on the variable being set - returning relevant output.
Here's more or less what I mean/want:
--variable not set, returns not substituted string:
SELECT 'solution=:abc' result;
result
---------------
solution=:abc
--variable set:
\set abc 5+7
SELECT 'solution=:abc' result;
result
---------------
solution=5+7
BUT INSTEAD in both cases variable doesn't get substituted:
SELECT 'solution=:abc' result;
result
---------------
solution=:abc
--variable set:
\set abc 5+7
SELECT 'solution=:abc' result;
result
---------------
solution=:abc
I found that I need to use :'variable' syntax, so
SELECT 'solution=':'abc' result;
but this gives an extra ('
) sign there:
result
---------------
solution='5+7
and doesn't work when variable is not set
\unset abc
SELECT 'solution=':'abc' result;
ERROR: syntax error at or near ":"
LINE 1: SELECT 'solution=':'abc' result;
What's going on?
Any way to make it work in both cases as described at the top? I am on PostgreSQL 9.6.11