Yes there is returning
INSERT INTO tag ("key", "value") SELECT 'key1', 'value1' WHERE NOT EXISTS ( SELECT id, "key", "value" FROM node_tag WHERE key = 'key1' AND value = 'value1' ) returning id, "key", "value"
To return a string if it already exists
with s as ( select id, "key", "value" from tag where key = 'key1' and value = 'value1' ), i as ( insert into tag ("key", "value") select 'key1', 'value1' where not exists (select 1 from s) returning id, "key", "value" ) select id, "key", "value" from i union all select id, "key", "value" from s
If the row does not exist, it will return the inserted other existing one.
BTW, if the key / value pair makes it unique, then this is the primary key, and there is no need for an id column. If one or both of the key / value pairs cannot be empty.
Clodoaldo Neto Aug 12 '13 at 17:14 2013-08-12 17:14
source share