CREATE OR REPLACE FUNCTION iszl.INSURED_POLICY_RECORD_autogenerated()
  RETURNS trigger AS
$BODY$
BEGIN
	UPDATE iszl.t_insured
	SET autogenerated = FALSE
	where InsuredID = New.Insured;
	Return New;
END;
$BODY$
LANGUAGE  plpgsql;

DO 
$$ 
BEGIN 
if not exists(select * from information_schema.triggers WHERE event_object_table = 't_insured_policy_record' AND trigger_name = 'insured_policy_record_autogenerated') 
then
	CREATE TRIGGER insured_policy_record_autogenerated
		After INSERT OR UPDATE of Insured
		ON ISZL.T_INSURED_POLICY_RECORD
		FOR EACH ROW
		EXECUTE PROCEDURE iszl.INSURED_POLICY_RECORD_autogenerated();
end if; 
END
$$ LANGUAGE  plpgsql;
