Can’t we use CASE condition outside SQL SELECT statements?
E.g.:
CASE
WHEN old.applies_to = 'admin' THEN _applies_to = 'My Self'
ELSE _applies_to = initcap(old.applies_to)
END
_summary = _summary || '<li>Apply To: ' || _applies_to || '</li>';
I get the following error:
ERROR: syntax error at or near "_summary"
LINE 86: _summary = _summary || '<li>Apply To: ' || _applies ...
This concerns the conditional control structure
CASEof the procedural language PL/pgSQL, to be used in PL/pgSQL functions, procedures, orDOstatements.Not to be confused with the
CASEexpression of SQL. Different language! Subtly different syntax rules.While SQL
CASEcan be embedded in SQL expressions inside PL/pgSQL code, you cannot have stand-alone SQLCASEexpressions (would be nonsense).You must use PL/pgSQL statements, terminated with a semicolon (
;) andEND CASE;to close it.Is
ELSErequired?PL/pgSQL
CASEexpects anELSEbranch and raises an error if it’s missing when reached. The manual:You can use an empty
ELSE:This is different for SQL
CASEwhereELSEis optional. If theELSEkeyword is present an expression must be given. WithoutELSE, it defaults toNULL. Like: