使用.NEW关键字创建复合触发器

时间:2017-12-04 22:56:49

标签: sql database oracle plsql triggers

我在使用Toad Datapoint 4在Oracle 11中创建触发器时遇到问题。我正在尝试创建一个复合触发器。

但是,每当我执行触发器的Create语句时,TOAD都会要求我将变量绑定到所有

:NEW.Id;
:NEW.Audit_Field1;

列。并且使用非法变量赋值名称失败。我只是尝试使用此触发器执行单个插入语句。如何创建此触发器?

CREATE TABLE ETRAT.SKU_Override 
( 
            Id NUMBER(10,0) NOT NULL ENABLE, 
          Audit_Field1 varchar(30) NOT NULL ENABLE, 
          Audit_Field2 TIMESTAMP (6) WITH TIME ZONE, 
          Audit_Field3 VARCHAR2(10 CHAR), 
          Audit_Field4 VARCHAR2(40 CHAR), 
          Char_Field5 char(1)
);

ALTER TABLE ETRAT.SKU_OVERRIDE ADD 
CONSTRAINT IX_SKU_OVERRIDE_PK PRIMARY KEY (Id);


CREATE TABLE ETRAT.SKU_OVERRIDE_HIST
( 
          Id NUMBER(10,0) NOT NULL ENABLE, 
          Audit_Field1 varchar(30) NOT NULL ENABLE, 
          Audit_Field2 TIMESTAMP (6) WITH TIME ZONE, 
          Audit_Field3 VARCHAR2(10 CHAR), 
          Audit_Field4 VARCHAR2(40 CHAR), 
          Char_Field5 char(1),
      Action varchar(10)
);
commit;

CREATE OR REPLACE TRIGGER SKU_OVERRIDE_HIST
FOR INSERT OR UPDATE OR DELETE
ON ETRAT.SKU_Override
COMPOUND TRIGGER
  TYPE t_emp_changes       IS TABLE OF ETRAT.SKU_OVERRIDE_HIST%ROWTYPE INDEX BY SIMPLE_INTEGER;
  v_emp_changes            t_emp_changes;
  v_index                  SIMPLE_INTEGER       := 0;
  v_threshhold    CONSTANT SIMPLE_INTEGER       := 1000; --maximum number of rows to write in one go.
  v_user          VARCHAR2(50); --logged in user

  PROCEDURE flush_logs
  IS
    v_updates       CONSTANT SIMPLE_INTEGER := v_emp_changes.count();
  BEGIN
    FORALL v_count IN 1..v_updates
        INSERT INTO SKU_OVERRIDE_HIST VALUES v_emp_changes(v_count);
    v_emp_changes.delete();
    v_index := 0; --resetting threshold for next bulk-insert.
  END flush_logs;

  AFTER EACH ROW
  IS
  BEGIN
      IF INSERTING THEN
        v_index := v_index + 1;
        v_emp_changes(v_index).Id               := :NEW.Id;
        v_emp_changes(v_index).Audit_Field1     := :NEW.Audit_Field1;
        v_emp_changes(v_index).Audit_Field2     := :NEW.Audit_Field2;
        v_emp_changes(v_index).Audit_Field3     := :NEW.Audit_Field3;
        v_emp_changes(v_index).Audit_Field4     := :NEW.Audit_Field4;
        v_emp_changes(v_index).Char_Field5      := :NEW.Char_Field5;
        v_emp_changes(v_index).Action := 'INSERT';
      ELSIF UPDATING THEN
        v_index := v_index + 1;

        v_emp_changes(v_index).Action := 'UPDATE';
      ELSE 
        v_index := v_index + 1;

        v_emp_changes(v_index).Action := 'DELETE';
END IF;

   END AFTER EACH ROW;

  -- AFTER STATEMENT Section:
  AFTER STATEMENT IS
  BEGIN
     flush_logs();
  END AFTER STATEMENT;

END SKU_OVERRIDE_HIST;
/

INSERT INTO ETRAT.SKU_OVERRIDE
(Id, AUDIT_FIELD1, AUDIT_FIELD2, AUDIT_FIELD3, AUDIT_FIELD4, CHAR_FIELD5)
VALUES (2, 'AUDIT_FIELD1',  null, 'AUDIT_FIE','AUDIT_FIELD', 'C');
COMMIT;

0 个答案:

没有答案