postgresql - INSERT or return row if entry already exists in a table with UNIQUE constraint -
Greetings,
I have this table, which is on PostGrace SQL Server 98:
create a table tag (large number of primary keys, not name text, value text not unique, unique (name, value));
Normal INSERT behavior is to throw an error when the new value breaks the specificity barrier. I would not like to throw the error for it or return the new tagad if the insertion has been successful, or the taglessness of the specific entry will match the specification.
I use this function to do this:
enter the enter function (my_name text, my_value text) $$ returns as DECLARE retval bigint; Tag from the beginning of the tag from where name = my_name and value = my_value; If returned again; end if; Include tags (name, value) values (my_name, my_value) Returning tagging in return; Returns the returns; End; $$ LANGUAGE plpgsql;
See two tables before the inclusion in the worst case. Is there a better way to do this possibly in a lookup?
Simply handling INSERT and some exceptions:
Insert FUNCTIONS Or change (my_name text, my_value text) $$ DECLARE retval returns as bigint; Enter the BEGIN tags (name, value) values (my_name, my_value) again in reversing; Returns the returns; Exception when unique_iolation then select the tag in the last name from the tag where the name = my_name and value = my_value; Returns the returns; End; $$ LANGUAGE plpgsql;
You can find more information in this manual:
Comments
Post a Comment