今天在写一个触发器的时候遇到一个问题,因为要用到新插入表中的数据进行查询,所以就遇到了一个问题:
解决办法是在DECLARE中加上一句话:PRAGMA AUTONOMOUS_TRANSACTION
但是这样又会遇到一个问题:
解决办法是在DML语句后面加上COMMIT
附上语句如下:
CREATE OR REPLACE TRIGGER "TR_CMS_MEMBERINFO"
AFTER INSERT OR UPDATE ON MEMBERNAME
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
ROWCOUNT INTEGER;
CLTPARENTNO VARCHAR2(30);
BEGIN
SELECT COUNT(*) INTO ROWCOUNT FROM MEMBERNAME WHERE CLTNO = :NEW.CLTNO;
IF ROWCOUNT > 0 THEN
BEGIN
BEGIN
SELECT CLTNO INTO CLTPARENTNO FROM MEMBERNAME WHERE ID = :NEW.PARENT_ID;
EXCEPTION WHEN NO_DATA_FOUND THEN
CLTPARENTNO := NULL;
END;
UPDATE ZKNMEMBER SET PARENTNO = CLTPARENTNO,LASTUPDATETIME = SYSDATE WHERE CLTNO = :NEW.CLTNO;
COMMIT;
END;
ELSE
BEGIN
BEGIN
SELECT CLTNO INTO CLTPARENTNO FROM MEMBERNAME WHERE ID = :NEW.PARENT_ID;
EXCEPTION WHEN NO_DATA_FOUND THEN
CLTPARENTNO := NULL;
END;
INSERT INTO ZKNMEMBER (CLTNO,PARENTNO,FEEMETHOD,FEEDIR1,FEEDIR2,CREATETIME,LASTUPDATETIME) VALUES (:NEW.CLTNO,CLTPARENTNO,1,1,1,SYSDATE,SYSDATE);
COMMIT;
END;
END IF;
END;
另外附上一篇关于触发器详解的博文:http://blog.csdn.net/indexman/article/details/8023740/