postgresql - 如何将查询加载到复合类型数组中?
问题描述
我有一个包含 3 列和多行的查询。我需要将查询结果存储到复合类型的数组中。有人可以帮助如何使用 array_agg 或其他方法而不是使用循环来实现它吗?
CREATE TYPE type1 AS
(
id bigint,
name character varying(4000),
created_on date
);
do
$$
declare
arr_type1 type1[];
--Query is select st_id, st_name, recorded_dt from st_samples;
end;
$$
language plpgsql;
我需要将上述注释查询中的数据加载到arr_type1
数组中。提前致谢。
解决方案
这可能是您正在寻找的:
do $$
declare
t type1[];
begin
select array_agg(row(id,name,recorded_dt)::type1) into t from st_samples;
raise notice '%',t;
end;
$$
不做任何有成效的事情,但 t 保存 type1 的数组并显示在输出中。
推荐阅读
- reactjs - typescript:redux connect 提供的 props 是一个不允许访问操作的交集
- algorithm - 试图理解为什么使用 BFS 搜索网格的空间复杂度是 O(m * n),其中 m 不是。行数,n 为否。列数
- ios - 如何将控制台输出打印到文件和控制台
- macos - 如何重新绑定 Mac 快捷键?
- python - python库图像处理-查找图像撕裂
- arrays - 拖放时如何自动优化Excel公式?
- jquery - Jquery:使用基于下拉选择的值自动填充多个输入字段
- r - 如何修复 R 中 ncdf4 并行处理中的 C 函数 R_nc4_get_vara_double 返回错误
- mysql - PDO 查询返回多个数组。如何唯一标识它们?
- javascript - .map 不是函数 reactjs