\else
这组命令实现了可嵌套的条件块。条件块必须以 \if 开始,并以 \endif 结束。在两者之间可以有任意数量的 \elif 子句,后面可以选择性地跟着一个 \else 子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。
当前查看 PostgreSQL 18。
中文定义与说明
这组命令实现了可嵌套的条件块。条件块必须以
\if开始,并以\endif结束。在两者之间可以有任意数量的\elif子句,后面可以选择性地跟着一个\else子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。\if和\elif命令读取其参数,并将其作为布尔表达式求值。如果表达式的值为true,则正常继续处理;否则,跳过后续行,直到遇到匹配的\elif、\else或\endif。一旦\if或\elif测试成功,同一块中后续\elif命令的参数就不再求值,而是被视为假。只有前面所有匹配的\if和\elif测试都未成功时,才会处理\else后面的行。与其他反斜线命令的参数一样,
\if或\elif命令的expression参数会经过变量插值和反引号扩展。随后,按开/关选项变量值的规则对结果求值。因此,有效值是以下值的不区分大小写且无歧义的匹配:true、false、1、0、on、off、yes、no。例如,t、T和tR都会被视为true。不能正确求值为真或假的表达式会产生警告,并被视为假。
被跳过的行会被正常解析以识别查询和反斜线命令,但查询不会发送到服务器,而且除了条件命令(
\if、\elif、\else、\endif)之外的反斜线命令会被忽略。仅检查跳过行中条件命令的有效嵌套。跳过行中的变量引用不会被展开,也不会执行反引号扩展。同一个条件块的所有反斜线命令必须出现在同一个源文件中。如果主输入文件或通过
\include引入的文件到达 EOF 时,仍有本地\if块未关闭,psql 就会报错。下面是一个示例:
-- 检查数据库中是否存在两条不同的记录,并将 -- 结果分别存入不同的 psql 变量 SELECT EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer, EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee \gset \if :is_customer SELECT * FROM customer WHERE customer_id = 123; \elif :is_employee \echo 'is not a customer but is an employee' SELECT * FROM employee WHERE employee_id = 456; \else \if yes \echo 'not a customer or employee' \else \echo 'this will never print' \endif \endif
说明
这组命令实现了可嵌套的条件块。条件块必须以 \if 开始,并以 \endif 结束。在两者之间可以有任意数量的 \elif 子句,后面可以选择性地跟着一个 \else 子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。
- 客户端
- psql 18.7
- 区分大小写的写法
- \else
用法
\if expression \elif expression \else \endif手册定义
这组命令实现了可嵌套的条件块。条件块必须以
\if开始,并以\endif结束。在两者之间可以有任意数量的\elif子句,后面可以选择性地跟着一个\else子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。\if和\elif命令读取其参数,并将其作为布尔表达式求值。如果表达式的值为true,则正常继续处理;否则,跳过后续行,直到遇到匹配的\elif、\else或\endif。一旦\if或\elif测试成功,同一块中后续\elif命令的参数就不再求值,而是被视为假。只有前面所有匹配的\if和\elif测试都未成功时,才会处理\else后面的行。与其他反斜线命令的参数一样,
\if或\elif命令的expression参数会经过变量插值和反引号扩展。随后,按开/关选项变量值的规则对结果求值。因此,有效值是以下值的不区分大小写且无歧义的匹配:true、false、1、0、on、off、yes、no。例如,t、T和tR都会被视为true。不能正确求值为真或假的表达式会产生警告,并被视为假。
被跳过的行会被正常解析以识别查询和反斜线命令,但查询不会发送到服务器,而且除了条件命令(
\if、\elif、\else、\endif)之外的反斜线命令会被忽略。仅检查跳过行中条件命令的有效嵌套。跳过行中的变量引用不会被展开,也不会执行反引号扩展。同一个条件块的所有反斜线命令必须出现在同一个源文件中。如果主输入文件或通过
\include引入的文件到达 EOF 时,仍有本地\if块未关闭,psql 就会报错。下面是一个示例:
-- 检查数据库中是否存在两条不同的记录,并将 -- 结果分别存入不同的 psql 变量 SELECT EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer, EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee \gset \if :is_customer SELECT * FROM customer WHERE customer_id = 123; \elif :is_employee \echo 'is not a customer but is an employee' SELECT * FROM employee WHERE employee_id = 456; \else \if yes \echo 'not a customer or employee' \else \echo 'this will never print' \endif \endif
相关条目
文档与源码
来源构建
- 版本
- 18.7
- 构建
- https://ftp.postgresql.org/pub/source/v18.7/postgresql-18.7.tar.bz2
- 来源指纹
1b2d62b4845aa46374f2cb680f95d3392322cc8128646992ae4e18d0e7084915
版本比较
PostgreSQL 18 → 19: 属性变化。
以下差异保留原始字段名与英文源描述。
--- PostgreSQL 18
+++ PostgreSQL 19
@@ -1,5 +1,5 @@
{
- "definition": "This group of commands implements nestable conditional blocks. A conditional block must begin with an \\if and end with an \\endif . In between there may be any number of \\elif clauses, which may optionally be followed by a single \\else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block. The \\if and \\elif commands read their argument(s) and evaluate them as a Boolean expression. If the expression yields true then processing continues normally; otherwise, lines are skipped until a matching \\elif , \\else , or \\endif is reached. Once an \\if or \\elif test has succeeded, the arguments of later \\elif commands in the same block are not evaluated but are treated as false. Lines following an \\else are processed only if no earlier matching \\if or \\elif succeeded. The expression argument of an \\if or \\elif command is subject to variable interpolation and backquote expansion, just like any other backslash command argument. After that it is evaluated like the value of an on/off option variable. So a valid value is any unambiguous case-insensitive match for one of: true , false , 1 , 0 , on , off , yes , no . For example, t , T , and tR will all be considered to be true . Expressions that do not properly evaluate to true or false will generate a warning and be treated as false. Lines being skipped are parsed normally to identify queries and backslash commands, but queries are not sent to the server, and backslash commands other than conditionals ( \\if , \\elif , \\else , \\endif ) are ignored. Conditional commands are checked only for valid nesting. Variable references in skipped lines are not expanded, and backquote expansion is not performed either. All the backslash commands of a given conditional block must appear in the same source file. If EOF is reached on the main input file or an \\include -ed file before all local \\if -blocks have been closed, then psql will raise an error. Here is an example: -- check for the existence of two separate records in the database and store -- the results in separate psql variables SELECT EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer, EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee \\gset \\if :is_customer SELECT * FROM customer WHERE customer_id = 123; \\elif :is_employee \\echo 'is not a customer but is an employee' SELECT * FROM employee WHERE employee_id = 456; \\else \\if yes \\echo 'not a customer or employee' \\else \\echo 'this will never print' \\endif \\endif",
+ "definition": "This group of commands implements nestable conditional blocks. A conditional block must begin with an \\if and end with an \\endif . In between there may be any number of \\elif clauses, which may optionally be followed by a single \\else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block. The \\if and \\elif commands read their argument(s) and evaluate them as a Boolean expression. If the expression yields true then processing continues normally; otherwise, lines are skipped until a matching \\elif , \\else , or \\endif is reached. Once an \\if or \\elif test has succeeded, the arguments of later \\elif commands in the same block are not evaluated but are treated as false. Lines following an \\else are processed only if no earlier matching \\if or \\elif succeeded. The expression argument of an \\if or \\elif command is subject to variable interpolation and backquote expansion, just like any other backslash command argument. After that it is evaluated like the value of an on/off option variable. So a valid value is any unambiguous case-insensitive match for one of: true , false , 1 , 0 , on , off , yes , no . For example, t , T , and tR will all be considered to be true . Expressions that do not properly evaluate to true or false will generate a warning and be treated as false. Lines being skipped are parsed normally to identify queries and backslash commands, but queries are not sent to the server, and backslash commands other than conditionals ( \\if , \\elif , \\else , \\endif ) are ignored. Conditional commands are checked only for valid nesting. Variable references in skipped lines are not expanded, and backquote expansion is not performed either. All the backslash commands of a given conditional block must appear in the same source file. If EOF is reached on the main input file or an \\include -ed file before all local \\if -blocks have been closed, then psql will raise an error. Here is an example: -- check for the existence of two separate records in the database and store -- the results in separate psql variables SELECT EXISTS (SELECT 1 FROM customer WHERE customer_id = 123) AS is_customer, EXISTS (SELECT 1 FROM employee WHERE employee_id = 456) AS is_employee \\gset \\if :is_customer SELECT * FROM customer WHERE customer_id = 123; \\elif :is_employee \\echo 'is not a customer but is an employee' SELECT * FROM employee WHERE employee_id = 456; \\else \\if yes \\echo 'not a customer or employee' \\else \\echo 'this will never print' \\endif \\endif",
"modifiers": [],
"signature": "\\if expression \\elif expression \\else \\endif",
"spellings": [
比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。
相关条目
导出 JSON · 返回psql 命令 · 收录范围为 PostgreSQL 10 至 20;最早采样版本不一定是实际引入版本。