Possible Duplicate:
Insert, on duplicate update (postgresql)
Cannot SELECT from UPDATE RETURNING clause in postgres
Help to understand me syntax error. I try to make such implementation of upsert query in PosgreSql:
create table tbl( key int, val int);
insert into tbl(key,val)
select distinct(key), 0 from unnest('{0,1,2,3,4,5}'::int[]) as key
where key not in (
update tbl set val = 1
where key = any('{0,1,2,3,4,5}'::int[])
returning key
);
error is :
ERROR: syntax error at or near "tbl"
ROWS 6: update tbl set val = 1
^
********** Error **********
ERROR: syntax error at or near "tbl"
SQL state: 42601
Character: 167
But update subquery without insert part work well.
Is any easiest way to make upsert query?