0

How exactly do I get this to work? I am trying my best to form a query that takes the primary keys generated from the first query and then inserts them into the 2nd table along with a static 2nd value(33). I am obviously getting a "more than one row returned by a subquery used as an expression" error. I'm googled my eye balls out and can't figure out this issue. Maybe there's a better way to do what I am trying to do.

I am using Postgresql 9.5 if that matters.

 WITH x AS (INSERT INTO OPTIONS (manufacturer_id, category, name, description)
VALUES (
    UNNEST(ARRAY['10', '22', '33']),
    'ExtColor',
    UNNEST(ARRAY['EC', 'IC', 'IO']),
    UNNEST(ARRAY['a', 'b', 'c'])
)
RETURNING option_id)
INSERT INTO opt_car_data (car_id, option_id) VALUES ((SELECT option_id FROM x), 33);

1 Answer 1

1
WITH x AS (
    INSERT INTO options (manufacturer_id, category, name, description)
    VALUES (
        UNNEST(ARRAY['10', '22', '33']),
        'ExtColor',
        UNNEST(ARRAY['EC', 'IC', 'IO']),
        UNNEST(ARRAY['a', 'b', 'c'])
    )
    RETURNING option_id
)
INSERT INTO opt_car_data (car_id, option_id) 
SELECT option_id, 33 
FROM x;
Sign up to request clarification or add additional context in comments.

Comments

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.