user.delete
注意: - 我的要求是,我创建了一个视图“STATUS_ID_VEW” 表APPLICANT,Soi想要在表上发生任何变化时 对'APPLICANT_STATUS_ID'列的申请人然后记录插入 带有新旧APPLICANT_STATUS_ID的“STATUS_CHANGE”表 系统日期,我试过这种方式,但没有任何反应请建议。
答案 0 :(得分:1)
Datetime()不是oracle函数。请改用sysdate。我认为你错过了编译错误,并且没有编译触发器。
错误(2,1):PL / SQL:忽略SQL语句
错误(3,96):PL / SQL:ORA-00904:" DATETIME":无效标识符
从USER_OBJECTS中选择状态OBJECT_NAME =' TRI_STATUS_CHANGE';
您使用INSTEAD OF触发器。这意味着执行触发器代码而不是VIEW(基础TABLE)上的实际插入或更新。因此,您只能期望STATUS_CHANGE表中的更改不在APPLICANT表中。
执行:
CREATE TABLE "STATUS_CHANGE"
(
APPLICANT_ID NUMBER,
PARTNER_ID NUMBER,
OLD_APP_STATUS_ID NUMBER(3,0),
NEW_APP_STATUS_ID NUMBER(3,0),
MODIFIED_ON DATE
) ;
CREATE TABLE APPLICANT (
APPLICANT_ID NUMBER
,PARTNER_ID NUMBER
,APPLICANT_STATUS_ID NUMBER(3,0));
CREATE VIEW STATUS_ID_VEW
AS
SELECT APPLICANT_ID,PARTNER_ID,APPLICANT_STATUS_ID FROM APPLICANT;
CREATE OR REPLACE TRIGGER TRI_STATUS_CHANGE
INSTEAD OF INSERT OR UPDATE ON STATUS_ID_VEW
BEGIN
INSERT INTO STATUS_CHANGE(APPLICANT_ID, PARTNER_ID,OLD_APP_STATUS_ID,NEW_APP_STATUS_ID,MODIFIED_ON)
VALUES (:new.APPLICANT_ID,:old.PARTNER_ID, :new.APPLICANT_STATUS_ID, :old.APPLICANT_STATUS_ID, sysdate);
END;
/
insert into STATUS_ID_VEW values (1,1,1);
insert into APPLICANT values (2,2,2);
select * from STATUS_CHANGE;
select * from APPLICANT;
输出:
APPLICANT_ID PARTNER_ID OLD_APP_STATUS_ID NEW_APP_STATUS_ID MODIFIED_ON
------------ ---------- ----------------- ----------------- --------------------
1 1 27-MAY-2015 13:32:09
APPLICANT_ID PARTNER_ID APPLICANT_STATUS_ID
------------ ---------- -------------------
2 2 2
插入STATUS_ID_VEW只执行触发并仅在STATUS_CHANGE表(第一个输出记录)中的记录中生成
插入APPLICANT会绕过触发器并仅导致APPLICANT表中的记录(第二个输出记录)
修改强>
CREATE OR REPLACE TRIGGER TRI_STATUS_CHANGE
AFTER UPDATE OF APPLICANT_STATUS_ID ON APPLICANT
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
INSERT INTO STATUS_CHANGE(APPLICANT_ID, PARTNER_ID,OLD_APP_STATUS_ID,NEW_APP_STATUS_ID,MODIFIED_ON)
VALUES (:new.APPLICANT_ID,:old.PARTNER_ID, :new.APPLICANT_STATUS_ID, :old.APPLICANT_STATUS_ID, sysdate);
END;
/