Re: store multiple rows with the SELECT INTO statement

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Janek Sendrowski <janek12(at)web(dot)de>
Cc: "pgsql-general(at)postgresql(dot)org >> PG-General Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: store multiple rows with the SELECT INTO statement
Date: 2013-09-02 07:27:16
Message-ID: CAFj8pRD24r=o0U-vNi3m=ycc5JqfeSmAA=Qhfci+dXwZNyGvDg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)web(dot)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
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gianni Ceccarelli 2013-09-02 12:59:44 Re: SSI and predicate locks - a non-trivial use case
Previous Message Tom Lane 2013-09-02 02:37:07 Re: My Experiment of PG crash when dealing with huge amount of data