开发是用到的一个触发器,现学现卖,功能实现了,留此记录以便日后查阅。
CREATE OR REPLACE TRIGGER trigger_oa_mail_user
AFTER DELETE OR INSERT OR UPDATE ON oa_mail_user
for each row
BEGIN
IF INSERTING THEN
--INSERT触发
IF :new.NOTREAD = 1 THEN
IF :new.mailstatus = 1 and :new.mail_id is not null THEN
update oa_system_remind t set t.newmail = t.newmail+1 where t.userid = :new.emp_id;
END IF;
END IF;
ELSIF UPDATING THEN
--UPDATE触发
IF :new.NOTREAD = 1 THEN
IF (:old.mailstatus = 1) and (:new.mailstatus = 10 or :new.mailstatus = 11) THEN
update oa_system_remind t set t.newmail = t.newmail-1 where t.userid = :new.emp_id;
ELSIF :new.mailstatus = 1 and :old.mailstatus != 1 THEN
update oa_system_remind t set t.newmail = t.newmail+1 where t.userid = :new.emp_id;
ELSIF :new.mailstatus != 1 and :old.mailstatus = 1 THEN
update oa_system_remind t set t.newmail = t.newmail-1 where t.userid = :new.emp_id;
ELSIF :old.NOTREAD != 1 THEN
update oa_system_remind t set t.newmail = t.newmail+1 where t.userid = :new.emp_id;
END IF;
ELSIF :new.NOTREAD != 1 and :old.NOTREAD = 1 THEN
update oa_system_remind t set t.newmail = t.newmail-1 where t.userid = :new.emp_id;
END IF;
ELSIF DELETING THEN
--DELETE触发
IF :old.NOTREAD = 1 THEN
IF :old.mailstatus = 1 THEN
update oa_system_remind t set t.newmail = t.newmail-1 where t.userid = :old.emp_id;
END IF;
END IF;
END IF;
END;