Re: store multiple rows with the SELECT INTO statement - Mailing list pgsql-general

From Pavel Stehule
Subject Re: store multiple rows with the SELECT INTO statement
Date
Msg-id CAFj8pRD24r=o0U-vNi3m=ycc5JqfeSmAA=Qhfci+dXwZNyGvDg@mail.gmail.com
Whole thread Raw
In response to store multiple rows with the SELECT INTO statement  ("Janek Sendrowski" <janek12@web.de>)
Responses Re: store multiple rows with the SELECT INTO statement  (Kevin Grittner <kgrittn@ymail.com>)
List pgsql-general
Hello

PostgreSQL doesn't support a table variables, but you can use a arrays.

postgres=# create table foo2(a int, b int);
CREATE TABLE
postgres=# insert into foo2 select i, i+1 from generate_series(1,4) g(i);
INSERT 0 4
postgres=# select * from foo2;
 a | b
---+---
 1 | 2
 2 | 3
 3 | 4
 4 | 5
(4 rows)

postgres=# select array(select row(a,b) from foo2);
             ?column?             
-----------------------------------
 {"(1,2)","(2,3)","(3,4)","(4,5)"}
(1 row)
                                                             ^
postgres=# select * from unnest(array(select row(a,b) from foo2)) as (a int, b int);
 a | b
---+---
 1 | 2
 2 | 3
 3 | 4
 4 | 5
(4 rows)

or in plpgsql

postgres=# do $$
declare
  a foo2[] = array(select row(a,b) from foo2);
  r record;
begin
  for r in select * from unnest(a)
  loop
    raise notice '% %', r.a, r.b;
  end loop;
end;
$$;
NOTICE:  1 2
NOTICE:  2 3
NOTICE:  3 4
NOTICE:  4 5
DO


Regards

Pavel


2013/9/2 Janek Sendrowski <janek12@web.de>
Hi,
 
Why is it only possible to store one row by a query which returns multiple rows using the SELECT INTO statement.
and
How can I do a Query on a record varialbe, somehow like this:
SELECT * FROM v_rec
 
Janek Sendrowski

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: My Experiment of PG crash when dealing with huge amount of data
Next
From: Gianni Ceccarelli
Date:
Subject: Re: SSI and predicate locks - a non-trivial use case