关于 Unique_violation 异常如何更新或删除引发异常的行
表代码和插入
create table test
(
id serial not null,
str character varying NOT NULL,
is_dup boolean DEFAULT false,
CONSTRAINT test_str_unq UNIQUE (str)
);
INSERT INTO test(str) VALUES ('apple'),('giant'),('company'),('ap*p*le');
功能
CREATE OR REPLACE FUNCTION rem_chars()
RETURNS void AS
$BODY$
BEGIN
begin
update test set str=replace(str,'*','');
EXCEPTION WHEN unique_violation THEN
--what to do here to delete the row which raised exception or
--to update the is_dup=true to that row
end;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION rem_chars() OWNER TO postgres;