Re: Elegant way to insert of some value in some tables in short instruction
Andreas Kretschmer <[email protected]> Thu, 14 Mar 2019 10:28:38 +0100
| Newsgroups | gmane.comp.db.postgresql.novice |
|---|---|
| Message-ID | <[email protected]> |
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