CREATE OR REPLACE FUNCTION ifexist()
RETURNS integer AS
$BODY$
DECLARE
exist int;
BEGIN
select count(*) from users where name = 'mandy' into exist;
if exist > 0
then
update users set id = id;
return 1;
else
insert into users (id,name) values (exist+1,'mandy');
return -1;
end if;
return 0;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION ifexist() OWNER TO postgres;
--执行
select ifExists();
--删除函数
DROP FUNCTION ifExist()
--查看结果
select * from users