插入触发器修改一列并更新Oracle

时间:2015-09-24 07:35:07

标签: oracle triggers

/* Formatted on 24/09/2015 09:36:39 (QP5 v5.215.12089.38647) */
CREATE OR REPLACE TRIGGER TR_student_PREF_sub_UPDATE
   AFTER INSERT OR UPDATE
   ON student
   REFERENCING NEW AS NEW OLD AS OLD
   FOR EACH ROW
DECLARE
   v_preferred_sub_value   NUMBER (1);
   no_project_record       EXCEPTION;
BEGIN
   IF UPDATING OR INSERTING
   THEN
      IF (:NEW.is_sub = 0 AND :NEW.standard_id IS NOT NULL)
      THEN
         SELECT preferred_sub
           INTO v_preferred_sub_value
           FROM STANDARD
          WHERE standard_id = :NEW.standard_id;
      ELSIF (:NEW.is_sub = 1 AND :NEW.subject_id IS NOT NULL)
      THEN
         SELECT preferred_sub
           INTO v_preferred_sub_value
           FROM admin_subject.source
          WHERE source_id = :NEW.subject_id;
      END IF; /* v_preferred_sub_value IS NOT NULL THEN UPDATE preferred_sub VALUE IN student TABLE */

      IF (v_preferred_sub_value IS NOT NULL)
      THEN
         UPDATE student
            SET preferred_sub = v_preferred_sub_value
          WHERE student_id = :NEW.student_id;

         COMMIT;
      END IF;
   END IF;
EXCEPTION
   WHEN NO_PROJECT_RECORD
   THEN
      NULL;
END TR_student_PREF_sub_UPDATE;

进程:当用户从UI单击“保存操作”时,此调用java代码中的插入方法。在将值插入到主题表中时,触发调用并检查is_sub值(如果为0),然后从标准表中获取preferred_sub值。如果is_sub = 1,则从admin_subject.source模式表中获取preferred_sub。

基于v_preferred_sub_value,它正在尝试更新新插入行的主题表。 我从UI运行了插入操作,在调用触发器之后,我使用preferred_sub列检查了主题表,它显示了空值。

请帮助我,我在那里犯了什么错误。

student table have student_id, subject_id, standard_id and is_sub values and more.
standard table & student table are in same schema.
source is different schema which has grant all privileged.

1 个答案:

答案 0 :(得分:2)

将触发器更改为before触发器,并将确定的值分配给appriopriate:new伪列。数据库将负责其余的工作。

IF (v_preferred_sub_value IS NOT NULL)
THEN
     :NEW.preferred_sub = v_preferred_sub_value;
END IF;

现在,NEW.preferred_sub的值将放在表记录中。