geek2000
geek2000

Reputation: 481

PL/PGSQL: Store the result of a loop in a table

I want to store the result of the following loop in a table:

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

How can I do that?

Upvotes: 0

Views: 278

Answers (1)

user330315
user330315

Reputation:

You don't need a loop for that at all. In fact you don't even need a function for that:

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

Or if you want to create the table based on the query:

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

Upvotes: 1

Related Questions