3

I'd like to do something like this:

INSERT INTO table2(name)
  INSERT INTO table1(name, address) VALUES ('me', 'home')
  RETURNING name;

And then I would expect a new record in table2 with 'me' in name. This would work fine with a SELECT returning the values, but I'm having trouble with the nested insert.

5

Use a data modifying CTE:

with t1 (name) as (
  INSERT INTO table1 (name, address) VALUES ('me', 'home')
  returning name
)
insert into table2 (name)
select name 
from t1;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.