From: | Andreas Kretschmer <andreas(at)a-kretschmer(dot)de> |
---|---|
To: | pgsql-novice(at)lists(dot)postgresql(dot)org |
Subject: | Re: Elegant way to insert of some value in some tables in short instruction |
Date: | 2019-03-14 09:28:38 |
Message-ID: | ac135b18-0561-43ad-bf3c-967bd2d09f18@a-kretschmer.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Am 13.03.19 um 20:25 schrieb David Raymond:
>
> ...but inserts into different tables need to be done in different statements.
you can insert into 2 tables within 1 statement using writeable common
table expressions (wCTE):
test=# create table table1(a int, b int, c int);
CREATE TABLE
test=*# create table table2(a int, b int, c int);
CREATE TABLE
test=*# with x as (insert into table1 values (1,2,3) returning *) insert
into table2 select * from x;
INSERT 0 1
test=*# select * from table1;
a | b | c
---+---+---
1 | 2 | 3
(1 row)
test=*# select * from table2;
a | b | c
---+---+---
1 | 2 | 3
(1 row)
test=*# with x as (insert into table1 values (4,5,6) returning *) insert
into table2 select * from x;
INSERT 0 1
test=*# select * from table1;
a | b | c
---+---+---
1 | 2 | 3
4 | 5 | 6
(2 rows)
test=*# select * from table2;
a | b | c
---+---+---
1 | 2 | 3
4 | 5 | 6
(2 rows)
test=*#
Regards, Andreas
--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com
From | Date | Subject | |
---|---|---|---|
Next Message | Ila B. | 2019-03-17 17:05:56 | Problems wording a GROUP BY ranked query |
Previous Message | David Rowley | 2019-03-14 00:16:59 | Re: Parallel Aggregates for string_agg and array_agg |