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

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

百科 / psql 命令 / 查询执行

\gset

将当前查询缓冲区发送给服务器,并将查询输出存入 psql 变量(参见下面的 变量 )。要执行的查询必须恰好返回一行。该行的每一列分别存入一个变量,变量名与列名相同。例如:

当前查看 PostgreSQL 18.6。

说明

将当前查询缓冲区发送给服务器,并将查询输出存入 psql 变量(参见下面的 变量 )。要执行的查询必须恰好返回一行。该行的每一列分别存入一个变量,变量名与列名相同。例如:

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

用法

\gset [ prefix ]

本版手册中的写法

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

手册定义

\gset [ prefix ]

将当前查询缓冲区发送给服务器,并将查询输出存入 psql 变量(参见下面的变量)。要执行的查询必须恰好返回一行。该行的每一列分别存入一个变量,变量名与列名相同。例如:

=> SELECT 'hello' AS var1, 10 AS var2
-> \gset
=> \echo :var1 :var2
hello 10

如果指定了prefix,则会将该字符串加到查询的列名前面,以构成要使用的变量名:

=> SELECT 'hello' AS var1, 10 AS var2
-> \gset result_
=> \echo :result_var1 :result_var2
hello 10

如果某一列的结果为 NULL,则取消设置对应的变量,而不是设置它。

如果查询失败或没有恰好返回一行,则不会更改任何变量。

若当前查询缓冲区为空,则重新执行最近发送的查询。

相关条目

文档与源码

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

版本比较

PostgreSQL 12 → 13: 属性变化。

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

--- PostgreSQL 12
+++ PostgreSQL 13
@@ -1,5 +1,5 @@
 {
-  "definition": "Sends the current query buffer to the server and stores the query's output into psql variables (see Variables ). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset => \\echo :var1 :var2 hello 10 If you specify a prefix , that string is prepended to the query's column names to create the variable names to use: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset result_ => \\echo :result_var1 :result_var2 hello 10 If a column result is NULL, the corresponding variable is unset rather than being set. If the query fails or does not return one row, no variables are changed. If the current query buffer is empty, the most recently sent query is re-executed instead.",
+  "definition": "Sends the current query buffer to the server and stores the query's output into psql variables (see Variables below). The query to be executed must return exactly one row. Each column of the row is stored into a separate variable, named the same as the column. For example: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset => \\echo :var1 :var2 hello 10 If you specify a prefix , that string is prepended to the query's column names to create the variable names to use: => SELECT 'hello' AS var1, 10 AS var2 -> \\gset result_ => \\echo :result_var1 :result_var2 hello 10 If a column result is NULL, the corresponding variable is unset rather than being set. If the query fails or does not return one row, no variables are changed. If the current query buffer is empty, the most recently sent query is re-executed instead.",
   "modifiers": [],
   "signature": "\\gset [ prefix ]",
   "spellings": [

比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。

相关条目

导出 JSON · 返回psql 命令 · 收录范围为 PostgreSQL 10 至 20;最早采样版本不一定是实际引入版本。