1

I am working in Postgres 9.6 and would like to insert multiple rows in a single query, using an INSERT INTO query.

I would also like, as one of the values inserted, to select a value from another table.

This is what I've tried:

insert into store_properties (property, store_id) 
values 
  ('ice cream', select id from store where postcode='SW1A 1AA'),
  ('petrol', select id from store where postcode='EC1N 2RN')
;

But I get a syntax error at the first select. What am I doing wrong?

Note that the value is determined per row, i.e. I'm not straightforwardly copying over values from another table.

1 Answer 1

0

demo:db<>fiddle

insert into store_properties (property, store_id) 
values 
('ice cream', (select id from store where postcode='SW1A 1AA')),
('petrol', (select id from store where property='EC1N 2RN'))

There were some missing braces. Each data set has to be surrounded by braces and the SELECT statements as well.

I don't know your table structure but maybe there is another error: The first data set is filtered by a postcode column, the second one by a property column...

Sign up to request clarification or add additional context in comments.

1 Comment

@Richard You're welcome: I would appreciate if you could upvote as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.