Note that
SELECT * FROM some_relation
WHERE NOT EXISTS (SELECT 1 FROM some_relation);
will always give you an empty result. Either some_relation
is empty itself or if it is not empty SELECT 1 FROM some_relation
is not empty and therefore NOT EXISTS ...
always returns false and so no record is matching the WHERE
clause.
What you want is to have the VALUES
as a CTE. You can then reference the values from your INSERT
statement and in a SELECT
to compare those values to the result of the RETURNING
clause.
WITH
vals AS (
VALUES ('label')
),
inserted AS (
INSERT INTO fiche(label)
SELECT * FROM vals
ON CONFLICT (label) DO NOTHING
RETURNING label, id
)
SELECT
vals.column1,
inserted.id
FROM vals
LEFT JOIN inserted ON vals.column1 = inserted.label
This should give you a row for each row in your VALUES
clause and the second column will be NULL
if it was not inserted due to a conflict or the inserted ID otherwise.