I have two tables where relationship one to many, and I want INSERT to mytable some values and RETURNING multiple values INTO variables to use them later. Here example:
IF NOT EXISTS
(SELECT id FROM mytable WHERE label_id = xxx)
THEN
INSERT INTO mytable
(label_id, "name")
VALUES
(xxx,'foo'),
(xxx,'bar')
RETURNING
(SELECT id INTO mytable_id1 FROM mytable WHERE name = 'foo'),
(SELECT id INTO mytable_id2 FROM mytable WHERE name = 'bar');
ELSE
(SELECT id INTO mytable_id1 FROM mytable WHERE name = 'foo'),
(SELECT id INTO mytable_id2 FROM mytable WHERE name = 'bar');
END IF;
But I get always error:
ERROR: INTO specified more than once at or near "into"
I trying google problem and for example INSERT INTO ... RETURNING multiple columns (PostgreSQL) and Error: INTO specified more than once at or near "INTO"
unfortunately solutions it doesn't help for me (or I do something wrong). Any ideas?