gpt4 book ai didi

PostgreSQL 相当于 Oracle "bulk collect"

转载 作者:行者123 更新时间:2023-11-29 13:39:18 25 4
gpt4 key购买 nike

在 PostgreSQL 中是否存在一些方法可以像在 Oracle 中一样使用批量收集来创建语句?

Oracle 中的示例:

create or replace procedure prc_tst_bulk_test is

type typ_person is table of tb_person%rowtype;
v_tb_person typ_person;

begin

select *
bulk collect into v_tb_person
from tb_person;

-- make a selection in v_tb_person, for instance
select name, count(*) from v_tb_person where age > 50
union
select name, count(*) from v_tb_person where gender = 1

end;

最佳答案

在 PostgreSQL 10 中,您可以使用 array_agg:

declare
v_ids int[];
begin
select array_agg(id) INTO v_ids
from mytable1
where host = p_host;

--use v_ids...

end;

您将拥有一个数组,它可用于使用 unnest 从中进行选择:

select * from unnest(v_ids) where ...

关于PostgreSQL 相当于 Oracle "bulk collect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com