A
A
Arseny Sokolov2016-03-01 10:04:45
PostgreSQL
Arseny Sokolov, 2016-03-01 10:04:45

Insert in insert?

Is it possible to run a query like this:

INSERT INTO table1 (column1) VALUES ((INSERT INTO table2 (column1) VALUES ('test') RETURNING id))

I understand that this option cannot be executed right here, but maybe I don’t know something, I don’t want to just make extra “gestures” through the driver, I want to make an entry in 2 tables at once with one shot.
If you can't do that, then write it like that.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2016-03-01
@ArsenBespalov

CTE to the rescue

with table2_insert as (
INSERT INTO table2 (column1) VALUES ('test') RETURNING id
)
INSERT INTO table1 (column1) select id from table2_insert

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question