PL / PGSQL:将循环结果存储在表中

时间:2016-08-28 21:31:55

标签: postgresql loops stored-procedures plpgsql

我想将以下循环的结果存储在表中:

 DO $$ 
 DECLARE rec RECORD; 
 BEGIN 
     FOR rec IN SELECT c1 FROM t1 
     LOOP 
        SELECT foo(rec.c1);  
     END LOOP; 
 END; $$ 
 LANGUAGE 'plpgsql';

我该怎么做?

1 个答案:

答案 0 :(得分:1)

根本不需要循环。事实上,你甚至不需要一个功能:

insert into some_table (some_column) 
select foo(c1) 
from t1

或者,如果您想根据查询创建表格:

create table_some
as
select foo(c1) as some_column
from t1;