0

I'm trying to get the whole result of a query into a variable, so I can loop through it and make inserts.

I don't know if it's possible.

I'm new to postgre and procedures, any help will be very welcome.

Something like:

declare result (I don't know what kind of data type I should use to get a query);
select into result label, number, desc from data 

Thanks in advance!

Pri Santos
  • 409
  • 1
  • 7
  • 21
  • Your question body does not reflect any research effort from your side. Anyway, you may take a look on [this other SO thread](http://stackoverflow.com/questions/8296034/stored-function-with-temporary-table-in-postgresql). – Luis Quijada Aug 20 '13 at 14:45

1 Answers1

1

I think you have to read PostgreSQL documentation about cursors.

But if you want just insert data from one table to another, you can do:

insert into data2 (label, number, desc)
select label, number, desc
from data 

if you want to "save" data from query, you also can use temporary table, which you can create by usual create table or create table as:

create temporary table temp_data as
(
    select label, number, desc
    from data 
)

see documentation

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
  • Roman, thanks for answering! I need to query a database and save the result into a variable, and after that manipulate the data. Only after that I want to insert it into another table. That's why I needed to save the query into a variable. – Pri Santos Aug 23 '13 at 17:31