↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

百科 / psql 命令 / 脚本与帮助

\elif

这组命令实现了可嵌套的条件块。条件块必须以 \if 开始,并以 \endif 结束。在两者之间可以有任意数量的 \elif 子句,后面可以选择性地跟着一个 \else 子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。

当前查看 PostgreSQL 18.6。

说明

这组命令实现了可嵌套的条件块。条件块必须以 \if 开始,并以 \endif 结束。在两者之间可以有任意数量的 \elif 子句,后面可以选择性地跟着一个 \else 子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。

客户端
psql 18.6
区分大小写的写法
\elif

用法

\if expression \elif expression \else \endif

本版手册中的写法

命令手册中的后缀修饰符
\if此语法签名未列出
\elif此语法签名未列出
\else此语法签名未列出
\endif此语法签名未列出

手册定义

\if expression
\elif expression
\else
\endif

这组命令实现了可嵌套的条件块。条件块必须以\if开始,并以\endif结束。在两者之间可以有任意数量的\elif子句,后面可以选择性地跟着一个\else子句。在形成条件块的命令之间通常会出现普通查询和其他类型的反斜线命令。

\if 和 \elif 读取参数并将其作为布尔表达式求值。结果为真时继续正常处理,否则跳过后续行,直到遇到匹配的 \elif、\else 或 \endif。某个 \if 或 \elif 条件成立后,同一块中后续 \elif 的参数不再求值,直接视为假。只有之前匹配的 \if 和 \elif 均未成立,才处理 \else 后的行。

与其他反斜杠命令参数一样,\if 或 \elif 的表达式参数先进行变量插值和反引号展开,然后像开关选项变量一样求值。有效值为 true、false、1、0、on、off、yes、no 的任意无歧义、不区分大小写的匹配。例如 t、T 和 tR 都会视为真。

不能正确求值为真或假的表达式会产生警告,并被视为假。

被跳过的行会被正常解析以识别查询和反斜线命令,但查询不会发送到服务器,而且除了条件命令(\if、\elif、\else、\endif)之外的反斜线命令会被忽略。仅检查跳过行中条件命令的有效嵌套。跳过行中的变量引用不会被展开,也不会执行反引号扩展。

同一个条件块的所有反斜线命令必须出现在同一个源文件中。如果主输入文件或通过\include引入的文件到达 EOF 时,仍有本地\if块未关闭,psql就会报错。

下面是一个示例:

-- 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

相关条目

文档与源码

来源构建
版本
18.6
构建
https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8

版本比较

PostgreSQL 13 → 14: 属性变化。

以下差异保留原始字段名与英文源描述。

--- PostgreSQL 13
+++ PostgreSQL 14
@@ -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;最早采样版本不一定是实际引入版本。