如何使用 postgresql 将存储过程中的数据插入新的 table

How to insert data from stored procedure into a new table using postgresql

我使用 PGADMIN4 创建了一个存储过程。在 SP 中,我选择了一个视图 table。但是,我希望将存储过程中的数据插入到新的 table 中。我试过下面的代码,但显示错误:

SP 名称:测试

新table姓名:客户

Insert into public.Customer exec public.Test

这是我的 SP 代码:

create procedure test()
language plpgsql
as $$
BEGIN
Select * from public.customer_list; 

END; 
$$;

错误:“exec”处或附近的语法错误

过程不能在SQL中使用。如果你必须有一个存储过程(定义为存储在数据库中要执行的代码)然后更改为SQL函数。其中returns一种。

create function copy_customer_list()
  returns setof customer_list 
  language sql
as $$
   Select * from customer_list; 
$$;  

然后你可以用

插入其他table
insert into customer  
  select * from copy_customer_list();