I've got an old table I want to split into two, but I'd rather not run two near-identical queries across the whole thing. The following does not work because you can only return column from the table you're inserting into:
WITH comment_insert AS (
INSERT INTO comments (content)
SELECT content
FROM old_schema.comments
JOIN area ON area.id = old_schema.comments.area_id
RETURNING id, area.id AS area_id
)
INSERT INTO area_comment (parent_id, area_id)
SELECT comment_insert.id,
comment_insert.area_id
FROM transitional_schema.mon_area_comment
Is it possible to do something like this, putting some columns in the "parent" table and the parent ID plus some other columns into another table, using a single query?
Best Answer
Modelling scheme:
db<>fiddle here
Pay attention - in last INSERT both CTE and source table are used.