sql - where not exists error in upsert function -
i have gotten lot of writing strong upsert function postgresql. trying insert values table 3 columns, 2 foreign keys, , 1 text.
i have similar function works great, not have subqueries 1 does. getting error
"error: syntax error @ or near "where" line 24: not exists (select 1 sel)"
the function:
create or replace function upsert_dish_cluster_to_network(n_id int, c_id int, amsover text) returns setof dish_cluster_to_network $func$ begin loop begin return query sel ( select dctn.id dish_cluster_to_network dctn dctn.network_id = (select id unv_network amscode = n_id) , dctn.cluster_id = (select id dish_cluster_network axsyscode = c_id) share ), ins ( insert dish_cluster_to_network (network_id, cluster_id, amsname_override) values ( (select id unv_network amscode = n_id), (select id dish_cluster_network axsyscode = c_id), amsover ) not exists (select 1 sel) returning * ), upd ( update dish_cluster_to_network dctn set network_id = (select id unv_network amscode = n_id), cluster_id = (select id dish_cluster_network axsyscode = c_id), amsname_override = amsover sel sel.id = dctn.id , ( dctn.network_id distinct (select id unv_network amscode = n_id) or dctn.cluster_id distinct (select id dish_cluster_network axsyscode = c_id) ) returning dctn.* ) select * ins union select * upd; return; exception when unique_violation raise notice 'something bad happened'; end; end loop; end $func$ language plpgsql;
i cannot figure out why not have not exists line. if comment out line function inserts new entries , never updates.
there no where
clause insert command. replace values
select
ins ( insert dish_cluster_to_network ( network_id, cluster_id, amsname_override ) select (select id unv_network amscode = n_id), (select id dish_cluster_network axsyscode = c_id), amsover not exists (select 1 sel) returning * ),
Comments
Post a Comment