将选择查询的输出存储在 postgres 的一个数组中 [英] Store select query's output in one array in postgres

查看:41
本文介绍了将选择查询的输出存储在 postgres 的一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

SELECT column_name
FROM information.SCHEMA.columns
WHERE table_name = 'aean'

它返回表aean的列名.
现在我已经声明了一个数组:

It returns column names of table aean.
Now I have declared an array:

DECLARE colnames text[]

如何将 select 的输出存储在 colnames 数组中.
是否需要初始化colnames?

How can I store select's output in colnames array.
Is there any need to initialize colnames?

推荐答案

有两种方法.一种是聚合:

There are two ways. One is to aggregate:

SELECT array_agg(column_name::TEXT)
FROM information.schema.columns
WHERE table_name = 'aean'

另一种是使用数组构造函数:

The other is to use an array constructor:

SELECT ARRAY(
    SELECT column_name 
    FROM information_schema.columns 
    WHERE table_name = 'aean'
)

我假设这是针对 plpgsql 的.在这种情况下,您可以像这样分配它:

I'm presuming this is for plpgsql. In that case you can assign it like this:

colnames := ARRAY(
    SELECT column_name
    FROM information_schema.columns
    WHERE table_name='aean'
);

这篇关于将选择查询的输出存储在 postgres 的一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆