日期:2014-05-16 浏览次数:20544 次
Exp1: SQL> CREATE TABLE T(ID NUMBER(18),MC VARCHAR2(20),DT DATE); 表已创建。 SQL> CREATE OR REPLACE TRIGGER TR_T 2 AFTER DELETE ON T 3 FOR EACH ROW 4 DECLARE V_COUNT NUMBER; 5 --PRAGMA AUTONOMOUS_TRANSACTION; 6 BEGIN 7 INSERT INTO T VALUES(:OLD.ID,:OLD.MC,SYSDATE); 8 COMMIT; 9 END TR_DEL_CABLE; 10 / 触发器已创建 SQL> INSERT INTO T VALUES(1,'111111',SYSDATE); 已创建 1 行。 SQL> INSERT INTO T VALUES(2,'222222',SYSDATE); 已创建 1 行。 SQL> COMMIT; 提交完成。 SQL> SELECT ID,MC,TO_CHAR(DT,'YYYYMMDD HH24:MI:SS') FROM T; ID MC TO_CHAR(DT,'YYYYM ---------- -------------------- ----------------- 1 111111 20080802 11:07:36 2 222222 20080802 11:07:43 SQL> DELETE FROM T WHERE ID=1; DELETE FROM T WHERE ID=1 * 第 1 行出现错误: ORA-04091: 表 TEST.T 发生了变化, 触发器/函数不能读它 ORA-06512: 在 "TEST.TR_T", line 4 ORA-04088: 触发器 'TEST.TR_T' 执行过程中出错 SQL> SELECT ID,MC,TO_CHAR(DT,'YYYYMMDD HH24:MI:SS') FROM T; ID MC TO_CHAR(DT,'YYYYM ---------- -------------------- ----------------- 1 111111 20080802 11:07:36 2 222222 20080802 11:07:43 SQL> CREATE OR REPLACE TRIGGER TR_T 2 AFTER DELETE ON T 3 FOR EACH ROW 4 DECLARE V_COUNT NUMBER; 5 PRAGMA AUTONOMOUS_TRANSACTION; 6 BEGIN 7 INSERT INTO T VALUES(:OLD.ID,:OLD.MC,SYSDATE); 8 COMMIT; 9 END TR_DEL_CABLE; 10 / 触发器已创建 SQL> DELETE FROM T WHERE ID=1; 已删除 1 行。 SQL> COMMIT; 提交完成。 SQL> SELECT ID,MC,TO_CHAR(DT,'YYYYMMDD HH24:MI:SS') FROM T; ID MC TO_CHAR(DT,'YYYYM ---------- -------------------- ----------------- 2 222222 20080802 11:07:43 1 111111 20080802 11:08:32
CREATE OR REPLACE PACKAGE BOM_AUTONUMBER IS TYPE t_MAX_SEQNUMBER is table of number INDEX BY PLS_INTEGER; v_MAX_SEQNUMBER t_MAX_SEQNUMBER; end BOM_AUTONUMBER
CREATE OR REPLACE TRIGGER TR_BOM_AUTONUMBER_SEQNUMBER BEFORE INSERT ON BOM REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW DECLARE vNumber number; vBOMID number; BEGIN vNumber:= 0; vBOMID:= :New.BOMID; if not BOM_AUTONUMBER.v_MAX_SEQNUMBER.EXISTS(vBOMID) then SELECT GREATEST(nvl(Max(to_number(SEQ_NUMBER)),0), Count(*)) INTO vNumber FROM BOM Where ITEM = vBOMID; BOM_AUTONUMBER.v_MAX_SEQNUMBER(vBOMID) := nvl(vNumber, 0); end if; BOM_AUTONUMBER.v_MAX_SEQNUMBER(vBOMID) := BOM_AUTONUMBER.v_MAX_SEQNUMBER(vBOMID) + 1; :NEW.SEQ_NUMBER := BOM_AUTONUMBER.v_MAX_SEQNUMBER(vBOMID); END TR_BOM_ AUTONUMBER_SEQNUMBER;