Pri Santos
Pri Santos

Reputation: 409

Store the whole query result in variable using postgresql Stored procedure

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!

Upvotes: 0

Views: 7571

Answers (1)

roman
roman

Reputation: 117337

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

Upvotes: 1

Related Questions