PostgreSQL CASE 在函数中的使用
我们不能在 SQL SELECT
语句之外使用 CASE
条件吗?
例如:
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>';
我收到以下错误:
ERROR: syntax error at or near "_summary"
LINE 86: _summary = _summary || '<li>Apply To: ' || _applies ...
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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这涉及条件控制结构
过程语言的 CASE
PL/pgSQL,用于 PL/pgSQL 函数,过程 ,或DO
语句。不要与
CASE
混淆SQL 表达式。不同的语言!语法规则略有不同。虽然 SQL
CASE
可以嵌入到 PL/pgSQL 代码内的 SQL 表达式中,但您不能拥有独立的 SQLCASE
表达式(这是无意义的)。您必须使用 PL/pgSQL 语句,以分号 (
;
) 终止并使用END CASE;
来关闭它。是否需要
ELSE
?PL/pgSQL
CASE
需要一个ELSE
分支,如果在到达时缺少该分支,则会引发错误。 手册:您可以使用空的
ELSE
:这与 SQL
CASE
不同,其中ELSE
是可选的。如果存在ELSE
关键字,则必须给出表达式。如果没有ELSE
,则默认为NULL
。喜欢:This concerns the conditional control structure
CASE
of the procedural language PL/pgSQL, to be used in PL/pgSQL functions, procedures, orDO
statements.Not to be confused with the
CASE
expression of SQL. Different language! Subtly different syntax rules.While SQL
CASE
can be embedded in SQL expressions inside PL/pgSQL code, you cannot have stand-alone SQLCASE
expressions (would be nonsense).You must use PL/pgSQL statements, terminated with a semicolon (
;
) andEND CASE;
to close it.Is
ELSE
required?PL/pgSQL
CASE
expects anELSE
branch and raises an error if it's missing when reached. The manual:You can use an empty
ELSE
:This is different for SQL
CASE
whereELSE
is optional. If theELSE
keyword is present an expression must be given. WithoutELSE
, it defaults toNULL
. Like: