Oracle数据库触发器实例

柔情痞子 提交于 2019-11-27 02:37:08

需求:在修改表QDYTH.JZFPJCXX时,要实时记录表中的数据变动情况
协商后结果:创建新表记录数据变动情况
实现手段:数据库触发器

  -- 创建或修改触发器 TR_JZFPJCXX_2
    CREATE OR REPLACE TRIGGER TR_JZFPJCXX_2
      -- 触发条件
      BEFORE DELETE OR INSERT OR UPDATE
      -- 触发器对应表
      ON QDYTH.JZFPJCXX
      -- 按行操作
      FOR EACH ROW
    DECLARE
      V_TYPE VARCHAR2(10); -- 更新类型
    BEGIN
      --INSERT触发
      IF INSERTING THEN
        V_TYPE := 'INSERT';
        INSERT INTO QDYTH.JZFPJCXX_HIS
         VALUES
           (:NEW.C_1,
            :NEW.C_2,
            :NEW.C_3,
            :NEW.C_4,
            :NEW.C_5,
            :NEW.C_6,
            :NEW.C_7,
            :NEW.C_8,
            :NEW.C_9,
            :NEW.C_10,
            :NEW.C_11,
            :NEW.C_12,
            :NEW.C_13,
            :NEW.C_14,
            :NEW.C_15,
            :NEW.C_16,
            :NEW.C_17,
            :NEW.C_18,
            :NEW.C_19,
            :NEW.C_20,
            :NEW.C_21,
            :NEW.C_22,
            :NEW.C_23,
            :NEW.C_24,
            :NEW.AAC001,
            :NEW.ZGYLJFZT,
            :NEW.ZGYILJFZT,
            :NEW.JMYLJFZT,
            :NEW.JMYILJFZT,
            :NEW.SFM16ZS,
            :NEW.TSRQ,
            :NEW.SFZH,
            :NEW.XH,
            :NEW.ZGYLDY,
            :NEW.JMYLDY,
            :NEW.JMYILJFLB2017,
            :NEW.BHSL,
            :NEW.JMYLJF2016,
            :NEW.JMYLJF2017,
            :NEW.CTIME,
            :NEW.JMYILJFLB2018,
            :NEW.JMYLJF2018,
            :NEW.AAE013,
            :NEW.JZFPJCXXID,
            SYSDATE, -- 更新时间 
            V_TYPE); 
      --UPDATE触发
      ELSIF UPDATING THEN
         V_TYPE := 'UPDATE';
         INSERT INTO QDYTH.JZFPJCXX_HIS
         VALUES
           (:NEW.C_1,
            :NEW.C_2,
            :NEW.C_3,
            :NEW.C_4,
            :NEW.C_5,
            :NEW.C_6,
            :NEW.C_7,
            :NEW.C_8,
            :NEW.C_9,
            :NEW.C_10,
            :NEW.C_11,
            :NEW.C_12,
            :NEW.C_13,
            :NEW.C_14,
            :NEW.C_15,
            :NEW.C_16,
            :NEW.C_17,
            :NEW.C_18,
            :NEW.C_19,
            :NEW.C_20,
            :NEW.C_21,
            :NEW.C_22,
            :NEW.C_23,
            :NEW.C_24,
            :NEW.AAC001,
            :NEW.ZGYLJFZT,
            :NEW.ZGYILJFZT,
            :NEW.JMYLJFZT,
            :NEW.JMYILJFZT,
            :NEW.SFM16ZS,
            :NEW.TSRQ,
            :NEW.SFZH,
            :NEW.XH,
            :NEW.ZGYLDY,
            :NEW.JMYLDY,
            :NEW.JMYILJFLB2017,
            :NEW.BHSL,
            :NEW.JMYLJF2016,
            :NEW.JMYLJF2017,
            :NEW.CTIME,
            :NEW.JMYILJFLB2018,
            :NEW.JMYLJF2018,
            :NEW.AAE013,
            :NEW.JZFPJCXXID,
            SYSDATE, -- 更新时间 
            V_TYPE);
      --DELETE触发
      ELSIF DELETING THEN
        V_TYPE := 'DELETE';
        INSERT INTO QDYTH.JZFPJCXX_HIS
        VALUES
          (:OLD.C_1,
           :OLD.C_2,
           :OLD.C_3,
           :OLD.C_4,
           :OLD.C_5,
           :OLD.C_6,
           :OLD.C_7,
           :OLD.C_8,
           :OLD.C_9,
           :OLD.C_10,
           :OLD.C_11,
           :OLD.C_12,
           :OLD.C_13,
           :OLD.C_14,
           :OLD.C_15,
           :OLD.C_16,
           :OLD.C_17,
           :OLD.C_18,
           :OLD.C_19,
           :OLD.C_20,
           :OLD.C_21,
           :OLD.C_22,
           :OLD.C_23,
           :OLD.C_24,
           :OLD.AAC001,
           :OLD.ZGYLJFZT,
           :OLD.ZGYILJFZT,
           :OLD.JMYLJFZT,
           :OLD.JMYILJFZT,
           :OLD.SFM16ZS,
           :OLD.TSRQ,
           :OLD.SFZH,
           :OLD.XH,
           :OLD.ZGYLDY,
           :OLD.JMYLDY,
           :OLD.JMYILJFLB2017,
           :OLD.BHSL,
           :OLD.JMYLJF2016,
           :OLD.JMYLJF2017,
           :OLD.CTIME,
           :OLD.JMYILJFLB2018,
           :OLD.JMYLJF2018,
           :OLD.AAE013,
           :OLD.JZFPJCXXID,
           SYSDATE, -- 更新时间 
           V_TYPE);
      END IF;
    END;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!